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 / 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;
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 / 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')) {
@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 / app.php
Last active February 8, 2023 20:15
Redis configuration for Craft CMS
<?php
/**
* Yii Application Config
*
* Edit this file at your own risk!
*
* The array returned by this file will get merged with
* vendor/craftcms/cms/src/config/app/main.php and [web|console].php, when
* Craft's bootstrap script is defining the configuration for the entire
* application.
@juban
juban / index.php
Created July 8, 2019 10:10
Test Craft CMS version
$schemaVersion = Craft::$app->getInstalledSchemaVersion();
if (version_compare($schemaVersion, '3.2.0', '>=')) {
// Do something specific to Craft CMS 3.2 and upper
}
@juban
juban / index.twig
Last active February 5, 2024 08:46
Paginate any array within Craft CMS 3 templates
{% set someArrayToPaginate = [
{someProperty:"One"},
{someProperty:"Two"},
{someProperty:"Three"},
{someProperty:"Four"},
{someProperty:"Five"},
{someProperty:"Six"},
{someProperty:"Seven"},
{someProperty:"Eight"},
{someProperty:"Nine"},
@juban
juban / index.twig
Created July 11, 2017 16:27
Craft CMS - Redirect to first available child entity in a structure or channel section
{% set entry = craft.entries.slug('homepage').first() %}
{% redirect entry.workshopPages[0].url %}
@juban
juban / gist:e3f3d6f7555d38829b6bfcaf6fba6a7e
Created March 21, 2017 10:27
List of installed Elasticsearch plugins
http://host.name/_nodes?plugin=true
@juban
juban / yii2-create-activeDataProvider-from-model-relation
Created December 4, 2016 11:00
Yii2 - Create an ActiveDataProvider from a HasMany model relation
<?php
$applicationsDataProvider = new \yii\data\ActiveDataProvider([
'query' => $model->getRelation('relationName'),
'pagination' => [
'pageParam' => 'apps_page'
]
]);
?>