Skip to content

Instantly share code, notes, and snippets.

@LittleCoding
LittleCoding / mytheme.theme.php
Last active November 24, 2021 16:54
Adding template suggestions for page by node types in Drupal 8
<?php
/**
* Implementation of hook_theme_suggestions_HOOK_alter() for page.html.twig.
*/
function MYTHEME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
// Add template suggestions based on the current node entity content type.
$key = array_search('page__node', $suggestions);
if ($key !== FALSE && ($node = \Drupal::routeMatch()->getParameter('node'))) {
$content_type = $node->bundle();
array_splice($suggestions, ++$key, 0, array('page__node_type__%', 'page__node_type__'.$content_type));