Skip to content

Instantly share code, notes, and snippets.

@DejanBelic
Created February 25, 2017 07:01
Show Gist options
  • Save DejanBelic/dddd4fb0299bd41d5055eb782d001dbe to your computer and use it in GitHub Desktop.
Save DejanBelic/dddd4fb0299bd41d5055eb782d001dbe to your computer and use it in GitHub Desktop.
Naming templates based on page title in drupal 8.
function my_theme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
// Naming templates based on page title.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$translation = $node->getTranslation('en');
$suggestions[] = 'page__node__' . strtolower($translation->title->value);
$title = $node->getTitle();
foreach ($suggestions as &$replace) {
$replace = str_replace(' ', '_', $replace);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment