Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
It is currently possible to create a page template specific to a node, but not specific to a content type shared by multiple nodes. The latter would be far useful.
Proposed resolution
Incorporate the actions of the following hook into core.
function HOOK_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if (array_search('page__front', $suggestions) === FALSE ) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$content_type = $node->bundle();
if ($content_type) {
array_unshift($suggestions, 'page__bundle_' . $content_type);
}
}
}
}
Remaining tasks
User interface changes
API changes
Twig templates with page__bundle_[some content type].html.twig will be automatically picked up without the need for additional hooks. This could conflict with any implementations of the above hook.
Comments
Comment #2
Aki Tendo CreditAttribution: Aki Tendo commentedUpdating code snippet above to not provide the bundle on the front page, which confuses twig into loading the bundle template instead of the front_page template.
Comment #3
dawehnerNote: The snipped would ideally take into account the entity type as well.
Comment #4
joelpittetI wonder how this plays into layouts initiative?
Comment #5
Aki Tendo CreditAttribution: Aki Tendo commented@joelpittet I don't know. I'll need to look into that closer
@dawehner I believe the entity type is accounted for by $node->bundle but I'm not sure.
Anyone know where this hook is called from? I presume that's where I need to do this operation if it's to be part of core.
Changed the hook code above from appending with the [] operator to prepending with array_unshift. The reason is that any node specific templates that exist should be loaded before the content type templates.
Shaking this out using a hook implementation is turning out to be a good thing - I've now had to change the hook twice.
Comment #7
Ivan Berezhnov CreditAttribution: Ivan Berezhnov as a volunteer and at Drupal Ukraine Community for Levi9 commented