Skip to content

Instantly share code, notes, and snippets.

View drubb's full-sized avatar

Boris Böhne drubb

View GitHub Profile
@drubb
drubb / cookies.js
Last active October 24, 2024 09:44
Drupal behavior that supports opening cookie banner links of the module drupal/cookies inside ajax modals
(function (Drupal, once) {
Drupal.behaviors.cookies = {
attach: function (context, settings) {
once('cookies', 'body', context).forEach(function () {
// Callback function to execute when the cookies banner is added.
const callback = (mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(node => {
@drubb
drubb / parse-media.php
Last active June 7, 2024 09:42
Using a php generator to parse and process Drupal media entities. Executable using drush scr
<?php
use Drupal\media\Entity\Media;
// Get the ids of all media entities.
$media_ids = \Drupal::entityQuery('media')
->accessCheck(FALSE)
->execute();
// Load and process each media entity.
@drubb
drubb / fix-permissions.sh
Last active February 20, 2024 09:47
Simple script to fix file and directory permissions in Composer based Drupal installation. Suitable if web server and CLI user are members of the same group.
#!/bin/sh
# Exit the script if any statement returns a non-true return value.
set -e
# Goto the project root using Drush
root="$(drush dd)"
cd "$root"
cd ..
@drubb
drubb / EntityEditLinkTwigExtension.php
Last active February 6, 2024 08:46
Custom Twig extension to provide links to an entity edit form. Permissions are checked, the current page is added as destination, and the link is styled using a fontawesome icon in this example. Usage in Twig: {{ eel(node) }}, {{ eel(user) }} or similar.
<?php declare(strict_types=1);
namespace Drupal\my_module;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
@drubb
drubb / NewSettingsForm.php
Last active November 28, 2023 12:21
Simplified Drupal config forms since Drupal 10.2
<?php declare(strict_types = 1);
namespace Drupal\my_module\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\RedundantEditableConfigNamesTrait;
/**
* Configure mymodule settings for this site.
@drubb
drubb / SignupOpenContext.php
Last active November 21, 2023 08:44
Example for a Drupal custom node context, for use e.g. in block visibility settings
<?php
namespace Drupal\frs_blocks\Plugin\Context;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
@drubb
drubb / EditorshipBundle.php
Last active May 6, 2023 07:14
Backlinks with bundle class and Twig: Editorship bundle class providing a getter for all series nodes referencing the editorship entity, and Twig Template using this getter to render the series nodes below the editorship as backlinks. Uses the Twig Tweak module!
<?php
namespace Drupal\frs_entities\Entity\Bundle;
use Drupal\Core\Field\FieldItemList;
/**
* A bundle class for node entities.
*
* @property FieldItemList $field_content
@drubb
drubb / discontinue.php
Last active April 27, 2023 08:02
Simple Drush Script Example, would be executed using "drush scr series.php"
<?php
/**
* Scans for discontinued series titles, sets the discontinued flag and
* rebuilds the title using the last broadcast episode.
*/
// Get all series entities.
$series = \Drupal::entityTypeManager()
->getStorage('node')
@drubb
drubb / ajax.libraries.yml
Last active July 14, 2022 08:25
Update a Drupal text field using a media entity selected in a paragraph using entity browser.
script:
js:
js/script.js: {}
dependencies:
- core/drupal
- core/jquery
- core/once
@drubb
drubb / BackgroundBehavior.php
Last active April 19, 2024 08:52
Example for a paragraph behavior plugin
<?php
namespace Drupal\behaviors\Plugin\paragraphs\Behavior;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;