I know it should be very simple, I know it was working in Drupal 7 but I can't make it work in 8.

I tried this but it is not working. "page--path.html.twig" is not used, Drupal is using the "page.html.twig" template. The page I'm trying to theme is "example.com/path" . This page is just a view page. I'm not interested in theming the view, just the page markup around it.

  • 1
    If 'path' is an alias it won't work. – sarathkm Jul 13 '17 at 6:16
  • yeah, found the problem. It was an alias – Marius Ilie Jul 13 '17 at 6:35

Since its an alias,

For Drupal 8

In your THEME_NAME.theme file:

function THEME_NAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  // Checks for node pages. 
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $current_path = \Drupal::service('path.current')->getPath();
    $alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
    $alias = ltrim('/', $alias);
    $clean_alias = str_replace('/', '__', $alias);
    $suggestions[] = 'page__'.$clean_alias;

You would like to check out clive answer from here for drupal 7

Template file naming with alias

| improve this answer | |
  • Hi, shouldn't the hook's name be "THEME_NAME_theme_suggestions_page_alter"? – Óscar Gómez Alcañiz Mar 15 '19 at 10:22
  • updated yes it should be hook_theme_suggestions_HOOK_alter – sarathkm Mar 17 '19 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.