Skip to content

Instantly share code, notes, and snippets.

View jenitehan's full-sized avatar

Jeni Tehan jenitehan

View GitHub Profile
@jenitehan
jenitehan / MyModuleRouteSubscriber.php
Created February 3, 2022 19:44
Set Drupal group parameter on a views page route with a group ID contextual filter.
<?php
namespace Drupal\my_module\EventSubscriber;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\RouteCollection;
/**
* My module route subscriber.
@jenitehan
jenitehan / THEME.theme.php
Created September 14, 2017 14:53
Add Drupal image template suggestions by bundle and field.
<?php
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function THEME_theme_suggestions_image_alter(&$suggestions, $variables) {
// Get the directory and filename.
// Need to use dir and filename to form the URI, since trying to load the file by filename alone
// will skip files that might have been uploaded with the same name as another and had "_0" added to the end.
@jenitehan
jenitehan / duplicate_view_from_config.php
Last active July 3, 2017 16:40
Load a Drupal 8 config, override some settings, save new config.
<?php
// This will load the view from config, set a new name and ID, then change the vocabulary to tags.
$config = \Drupal::configFactory()->getEditable('views.view.blog_categories');
$config->setName('views.view.blog_categories_tags');
$config->set('id', 'blog_categories_tags');
$config->set('display.default.display_options.filters.vid.value', ['tags' => 'tags']);
$config->save(TRUE);
<?php
/**
* Implements template_preprocess_paragraph().
*/
function TEMPLATE_preprocess_paragraph(&$variables) {
$paragraph = $variables['paragraph'];
$bundle = $paragraph->bundle();
if ($bundle == 'mybundle') {
// do stuff
@jenitehan
jenitehan / themename.theme.php
Last active February 18, 2019 10:24
Alter Drupal 8 block template suggestions by bundle
<?php
function themename_theme_suggestions_block_alter(array &$suggestions, array $variables) {
$elements = $variables['elements'];
if (isset($elements['content']['#block_content'])) {
$bundle = $elements['content']['#block_content']->bundle();
if ($bundle == 'bundle_name') {
$suggestions[] = 'block__template_name';
}
@jenitehan
jenitehan / reboot.yml
Created January 28, 2016 13:56
Reboot server if required and wait for it to come back.
- name: Reboot system if required
command: shutdown -r now 'Rebooting to complete system upgrade' removes=/var/run/reboot-required
register: reboot_required
tags:
- apt
- updates
- name: Wait for server to come back.
local_action: wait_for port={{ ansible_ssh_port }} host="{{ ansible_ssh_host | default(inventory_hostname) }}" search_regex=OpenSSH delay=10
when: reboot_required.changed
@jenitehan
jenitehan / gist:c9529e529a8ffd975609
Last active August 29, 2015 14:01
Alter user VBO assign all user roles
<?php
/**
* Views Bulk Operations doesn't respect Role Delegation's permissions in the "Modify user roles"
* action and will offer all roles as options to add or remove from the users being edited. This form
* alter fixed that by checking which roles the user had permissions to assign and altering the options
* in the select element.
*
* This is no longer needed since I can just use the "Delegate roles" action provided by Role Delegation.
* I didn't realise that was there!