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.

Data model changes

Comments

Aki Tendo created an issue. See original summary.

Aki Tendo’s picture

Issue summary: View changes

Updating 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.

dawehner’s picture

Note: The snipped would ideally take into account the entity type as well.

joelpittet’s picture

Issue tags: -theme

I wonder how this plays into layouts initiative?

Aki Tendo’s picture

Issue summary: View changes

@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.