Skip to content

Instantly share code, notes, and snippets.

View drubb's full-sized avatar

Boris Böhne drubb

View GitHub Profile
@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;
@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 / CacheTimeout.php
Last active January 11, 2023 16:39
Invalidate Drupal cache tags on full hours / at midnight using stack middleware
<?php
namespace Drupal\mymodule\StackMiddleware;
use Drupal\Core\Cache\CacheTagsInvalidator;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\State\State;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
@drubb
drubb / NewBlock.php
Last active December 7, 2022 22:38
Drupal 9 block using node context. Works for node pages, node revision pages and node previews.
<?php
namespace Drupal\title_block\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a block rendering the title of the current node.
*
* @Block(
@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