Skip to content

Instantly share code, notes, and snippets.

@LittleCoding
Last active November 24, 2021 16:54
Show Gist options
  • Save LittleCoding/f3b392dabe1ada23b31d3d3e089ce6a1 to your computer and use it in GitHub Desktop.
Save LittleCoding/f3b392dabe1ada23b31d3d3e089ce6a1 to your computer and use it in GitHub Desktop.
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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment