Code Snippets

My local Git configuration

By mherchel, 18 June, 2022

These set the Git defaults.

this .gitignore is located in my home directory.



This .gitconfig is located within my home directory.

	name = Mike Herchel
	email =
	fileMode = false
	excludesfile = /Users/mherchel/.gitignore
	autocorrect = 1
    sadd = !sh -c 'cd -- ${GIT_PREFIX:-.} && git add -- "$@" && git status' --
		addst = "!f() { git add $@; git status; }; f"
	postBuffer = 157286400


My Zsh configuration

By mherchel, 18 June, 2022

I use Oh my Zsh to manage my shell. Below is my configuration.

            # Path to your oh-my-zsh installation.
export ZSH="/Users/mherchel/.oh-my-zsh"



source $ZSH/

export MAMP_PHP=/Applications/MAMP/bin/php/php8.1.1/bin
export PATH=$PATH:/Applications/MAMP/Library/bin
export PATH=/usr/local/bin:$PATH
alias php='/Applications/MAMP/bin/php/php8.1.1/bin/php -c "/Library/Application Support/appsolute/MAMP PRO/conf/php8.1.1.ini"'
alias pear='/Applications/MAMP/bin/php/php8.1.1/bin/pear'
alias pecl='/Applications/MAMP/bin/php/php8.1.1/bin/pecl'

alias got='git'
alias gti='git'
alias gi='git'
alias giit='git'
alias it='git'
alias gt='git'
alias sgit='git'

alias dreset="git reset && git checkout . && rm inter* && rm *.patch && git status"
alias qd="chmod -R 777 sites && rm -rf sites/default && git checkout . && drush si --db-url=sqlite://sites/ --account-pass=admin --site-name=Drupal -y"

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/" # This loads nvm
export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"


My settings.local.php

By mherchel, 15 June, 2022

I end up copying and pasting this from different projects. Please don't steal my database password.

  $databases['default']['default'] = array (
  'database' => 'fldc22pantheon',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',

$settings['container_yamls'][] = $app_root . '/sites/';
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['page'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
$config['system.logging']['error_level'] = 'verbose';
$settings['rebuild_access'] = TRUE;
$settings['skip_permissions_hardening'] = TRUE;
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
$settings['file_public_path'] = "sites/default/files";
$settings['file_private_path'] = '../files/private';
$settings['file_temporary_path'] = '/tmp';
$settings['hash_salt'] = 'duh';
$settings['config_sync_directory'] = '../config';
$config['stage_file_proxy.settings']['origin'] =  '';


This file goes within /sites/. Creating a new filename ensures that it doesn't revert when I update Drupal.
            # Local development services.
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
  http.response.debug_cacheability_headers: true
    debug: true
    auto_reload: true
    cache: false
    class: Drupal\Core\Cache\NullBackendFactory


Theming views output

By mherchel, 13 June, 2022

You may have noticed that Twig debug mode does not output the template suggestions for views twig templates. The issue is currently needs review. 

Never fear though! Views template suggestions work similar to what they did in D7.

Views twig templates naming

  • Display output: views-view.html.twigviews-view--VIEWNAME.html.twigviews-view--attachment.html.twigviews-view--VIEWNAME--attachment.html.twigviews-view--DISPLAYNAME.html.twigviews-view--VIEWNAME--DISPLAYNAME.html.twigviews-view--default.html.twigviews-view--default.html.twigviews-view--DISPLAYNAME-1.html.twigviews-view--VIEWNAME--DISPLAYNAME-1.html.twig
  • Style output: views-view-unformatted.html.twigviews-view-unformatted--VIEWNAME.html.twigviews-view-unformatted--DISPLAYNAME.html.twigviews-view-unformatted--VIEWNAME--DISPLAYNAME.html.twigviews-view-unformatted--default.html.twigviews-view-unformatted--default.html.twigviews-view-unformatted--DISPLAYNAME-1.html.twigviews-view-unformatted--VIEWNAME--DISPLAYNAME-1.html.twig
  • Row style output: views-view-fields.html.twigviews-view-fields--VIEWNAME.html.twigviews-view-fields--DISPLAYNAME.html.twigviews-view-fields--VIEWNAME--DISPLAYNAME.html.twigviews-view-fields--default.html.twigviews-view-fields--default.html.twigviews-view-fields--DISPLAYNAME-1.html.twigviews-view-fields--VIEWNAME--DISPLAYNAME-1.html.twig
  • Field Content: views-view-field.html.twigviews-view-field--FIELDNAME.html.twigviews-view-field--VIEWNAME.html.twigviews-view-field--VIEWNAME--FIELDNAME.html.twigviews-view-field--DISPLAYNAME.html.twigviews-view-field--DISPLAYNAME--FIELDNAME.html.twigviews-view-field--VIEWNAME--DISPLAYNAME.html.twigviews-view-field--VIEWNAME--DISPLAYNAME--FIELDNAME.html.twigviews-view-field--DISPLAYNAME-1.html.twigviews-view-field--DISPLAYNAME-1--FIELDNAME.html.twigviews-view-field--VIEWNAME--DISPLAYNAME-1.html.twigviews-view-field--VIEWNAME--DISPLAYNAME-1--FIELDNAME.html.twig

When in doubt, fire up a D7 site, and go into the theming section of the view, and see the template suggestions there.

Use Drupal image styles within tokens when creating metatags

By mherchel, 19 May, 2022

Within the Drupal Metatag module, you frequently want to assign images to the og:image or twitter:image metatags. However, you don't want to serve the original image.

You can chain the image style after the image token to serve an image style.

Replace field_image with your image field machine name, and replace my_custom_image_style with the machine name of your image style. This works for image fields and media fields.


Alt text:

Get properly formatted URL from link field within Twig

By mherchel, 18 May, 2022

If your link field has an internal path (eg /path/foo), if you output node.field_link.uri in your twig template, it’ll output internal:/path/foo into your markup. We need to convert this into a proper URL.

Within preprocess, you can get this with



Within twig, you can simply output

            {{ node.field_link.0.url }}


Setting active links (or breadcrumbs) within twig

By mherchel, 18 May, 2022

You can use Drupal twig’s path function to do logic to check what page you’re on (and set CSS clasess or breadcrumb text )

The logic will look something like this:

            {% if path('<current>') == path('view.my_interests.page_1') %}
    {% set breadcrumb_text = 'For You' %}
  {% elseif path('<current>') == path('view.topics.page_1') %}
    {% set breadcrumb_text = 'Prospect Interests' %}
  {% elseif path('<current>') == path('view.news_by_prospect.page_1') %}
    {% set breadcrumb_text = 'People' %}
  {% else %}
    {% set breadcrumb_text = 'Back' %}
{% endif %}

Printing out Unix date, NID, sticky, etc

By mherchel, 18 May, 2022

There’s a bunch of getXXX methods under the node object that are not viewable while PHP debugging. You can call these with {{ node.XXX.value }}.

Screenshot of items within node object taken in PHPStorm
            {# Created date #}
{{ node.created.value }}

{# Updated date #}
{{ node.changed.value }}

{# Node ID #}
{{ node.nid.value }}

{# UID #}
{{ node.uid.value }}

{# Status #}
{{ node.status.value }}

{# Node Type #}
{{ node.type.value }}

Check for permissions in Twig

By mherchel, 18 May, 2022

You can check for permissions in twig by using the snippet below.

But, how do we know what the value for the hasPermission() function should be?

The easiest way is to navigate to the admin permissions page, and inspect the checkbox for the permission you’d like to check for. Within there, you’ll see a name attribute that has the information you’re looking for.

Screenshot of markup with the permissions highlighted within the name attribute
            {% if user.hasPermission('create prospect content') %}
  <a class="button--add-team-member" href="/node/add/prospect">Add New Prospect</a>
{% endif %}

Disable theme caching

By mherchel, 18 May, 2022

This 1) Disables the need for you to clear the cache on every page reload after making a change in Twig 2) Enables "Twig debugging", which outputs the the name of the current template (and template suggestions) within HTML comments.

Uncomment the following in settings.php and move it to the bottom of the file.

            if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
  include $app_root . '/' . $site_path . '/settings.local.php';

Copy example.settings.local.php to settings.local.php and verify the following lines are uncommented to disable render and local page caches.

            $config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;

Make your file like below. This file is already enabled within the settings.local.php that you’ve just enabled.
            # Local development services.
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
  http.response.debug_cacheability_headers: true
    debug: true
    auto_reload: true
    cache: false
    class: Drupal\Core\Cache\NullBackendFactory

Twig ternary operators

By mherchel, 18 May, 2022
            {{ page.content_right ? 'has-content-right' : 'no-content-right' }}

{{ page.content_right ? 'has-content-right' }}

{{ page.content_right ?: 'no-content-right' }}