Skip to content

Instantly share code, notes, and snippets.

View juban's full-sized avatar

Alban Jubert juban

  • Grenoble, France
View GitHub Profile
@juban
juban / index.php
Last active February 23, 2021 11:34
Craft CMS 3 - Add a Field to a section fields layout programmatically
<?php
// To put somewhere in a module, plugin or content migration
// Get the entry type to add the new field to
$entryType = Craft::$app->getSections()->getSectionByHandle('someSectionHandle')->entryTypes[0];
$field = Craft::$app->getFields()->getFieldByHandle('someFieldHandle');
// Get current fieldLayout
@juban
juban / index.php
Last active February 23, 2021 11:35
Craft CMS 3 - Set activation and reset password emails urls according to user group or site
<?php
Event::on(
\craft\records\User::class,
\craft\records\User::EVENT_AFTER_UPDATE,
static function (\yii\db\AfterSaveEvent $event) {
$userRecord = $event->sender;
$user = Craft::$app->getUsers()->getUserById($userRecord->id);
$sites = Craft::$app->getSites();
if ($user->isInGroup('user')) {
OS Information: Darwin localhost.home 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64
DDEV-Local version v1.17.6
architecture amd64
db drud/ddev-dbserver-mariadb-10.3:v1.17.3
dba phpmyadmin:5
ddev-ssh-agent drud/ddev-ssh-agent:v1.17.0
docker 20.10.6
docker-compose 1.29.1
os darwin
router drud/ddev-router:v1.17.6
@juban
juban / CriticalCssController.php
Last active June 12, 2022 22:11
CriticalCssController console controller to export a rollup-plugin-critical compatible configuration file to use with Vite.
<?php
namespace modules\console\controllers;
use Craft;
use craft\console\Controller;
use craft\elements\Entry;
use craft\helpers\Console;
use craft\helpers\FileHelper;
use craft\helpers\Json;