Skip to content

Instantly share code, notes, and snippets.

View gheydon's full-sized avatar

Gordon Heydon gheydon

View GitHub Profile
@gheydon
gheydon / example_theme.info.yml
Last active December 14, 2023 05:32
How to load remote google fonts into CKeditor 5
name: Example
description: 'Example theme'
base theme: false
type: theme
package: example
core_version_requirement: ^10
ckeditor_stylesheets:
- //fonts.googleapis.com/css2?family=Public+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap
@gheydon
gheydon / module_name.services.yml
Last active September 21, 2023 02:39
Add the following to turn off Drupal 10.1+ Lazy Loading of agreggated CSS and JS. This is depreciated and will be removed in Drupal 11.
services:
asset.css.collection_optimizer:
class: Drupal\Core\Asset\CssCollectionOptimizer
arguments: [ '@asset.css.collection_grouper', '@asset.css.optimizer', '@asset.css.dumper', '@state', '@file_system' ]
asset.js.collection_optimizer:
class: Drupal\Core\Asset\JsCollectionOptimizer
arguments: [ '@asset.js.collection_grouper', '@asset.js.optimizer', '@asset.js.dumper', '@state', '@file_system']
@gheydon
gheydon / example.module
Last active November 29, 2022 22:38
Change the configuration for the token browser for a webform element.
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function example_form_webform_ui_element_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form["properties"]["type"]["#value"] == 'example_element') {
$form["token_tree_link"]["token_tree_link"]["#token_types"][] = 'node';
$form["token_tree_link"]["token_tree_link"]["#recursion_limit"] = 4;
}
}
@gheydon
gheydon / example.module
Last active August 24, 2022 04:46
When dealing with checkboxes, the #title that you use is sometimes not very good to be used with error messages. In this case you can add the '#element_title' to your element and it will replace the this title with so the original title can be used for the error messages
/**
* Implements hook_preprocess_HOOK().
*
* HOOK: 'form_element'
*/
function example_preprocess_form_element(&$variables) {
if (isset($variables['element']['#element_title'])) {
$variables['label']['#title'] = $variables['element']['#element_title'];
}
}
@gheydon
gheydon / ErrorPages.php
Created August 12, 2022 04:29
Condition to allow showing only on a 403/404 page.
<?php
namespace Drupal\example\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Http\RequestStack;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@gheydon
gheydon / regexp.txt
Created June 18, 2022 13:44
Youtube Id extraction regexp
(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)|(?:shorts)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})
@gheydon
gheydon / settings.php
Last active October 31, 2021 08:58
In Drupal 8 the request object is used to determine if the request is from a reverse proxy. How ever in some cases the headers X-Forwarded-* are not being transmitted correctly so Drupal can't determine if the session between the browser and the reverse proxy are not being correctly. In the case X-Forwarded-Proto is being setting to http instead…
if (PHP_SAPI !== 'cli' && $_SERVER['HTTP_X_FORWARDED_PORT'] == 443) {
$GLOBALS['request']->headers->set('X_FORWARDED_PROTO', 'https');
}
@gheydon
gheydon / example.module
Created March 18, 2021 23:27
Set the media entities thumbnail to an uploaded thumbnail.
<?php
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function example_media_presave(EntityInterface $entity) {
if ($entity->bundle() == 'resource') {
if (!$entity->get('field_thumbnail')->isEmpty()) {
$file = $entity->field_thumbnail->entity;
$entity->set('thumbnail', $file);
@gheydon
gheydon / example.module
Last active March 3, 2021 22:28
Adding addition suggestions on a template based upon the referring entity.
<?php
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function example_theme_suggestions_file_link_alter(array &$suggestions, array $variables) {
/** @var \Drupal\file\Entity\File $file */
$file = $variables['file'];
try {
@gheydon
gheydon / TaxonomyTermChildren.php
Last active March 1, 2021 23:27
Drupal condition to allow checking if a taxonomy term has any children.
<?php
namespace Drupal\example\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\ctools\ConstraintConditionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;