user3634184
user3634184

Reputation: 466

Drupal 8 override Theme Hook Page file suggestion

I know there are some tutorials online about how to do this, but for some reason I doesn't work.

I would like to add a file theme suggestion on the "Theme Hook: Page" level of the template with the name of the content type (this way I can let all items of this content type use this template).

So, I add this to my MYTHEME.theme.php file:

function MYTHEME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
   if ($node = \Drupal::request()->attributes->get('node')) {
       array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
    }
}

So you would expect the following output:

I already tried clearing the cache. I'm using Drupal 8.1

What am I missing here?

Upvotes: 0

Views: 1230

Answers (3)

user3634184
user3634184

Reputation: 466

My mistake was not with the function but with the filename. My file name was MYTHEME.theme.php but it should have been MYTHEME.theme.

That's why the code didn't work.

Upvotes: 0

Nazmus Sakib
Nazmus Sakib

Reputation: 61

I was able to achieve this by the following code, which seems somewhat same as your code

if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $suggestions[] = 'page__node__'.$node->bundle();
}

Upvotes: 0

code.rider
code.rider

Reputation: 1897

simply add this file in to you theme floder node--typeName.html.twig no need any function. now your content type should use this template

Upvotes: 0

Related Questions