Clarify state of Twig page template by content type

Created on 26 July 2024, about 1 month ago

Problem/Motivation

Drupal 10.2 did not automatically suggest nor detect Twig page templates for content types, purely based on the naming convention. You had to add a small function in your theme, as can be seen in the documentation below.

Drupal 10.3: It looks like you get suggestions, even without a hook_theme_suggestions_page_alter function, but that the function is still needed, for a content type specific Twig template to take effect ...

Drupal 11: It looks like both suggestion and detection works out of the box.

Maybe someone can confirm if this summary is correct?

How to add a page template for a content type

Drupal does not automatically suggest or detect Twig page templates for content types purely based on the naming convention.

Note: It looks like you get suggestions in Drupal 10.3 by default, but that the function below is still necessary to make the content type specific Twig template take effect ...

To add a page template for a content type, add this in your example.theme file (replace "example"):

/**
 * Implements hook_theme_suggestions_page_alter().
 */
function example_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
  $suggestions[] = 'page__' . $node->bundle();
  }
}

Now, you get suggestions in the source for content type as well. Before:

<!-- FILE NAME SUGGESTIONS:
   ▪️ page--node--336.html.twig
   ▪️ page--node--%.html.twig
   ▪️ page--node.html.twig
   ✅ page.html.twig
-->

After (with the file page--article.html.twig also created) in the source:

<!-- FILE NAME SUGGESTIONS:
   ✅ page--article.html.twig
   ▪️ page--node--336.html.twig
   ▪️ page--node--%.html.twig
   ▪️ page--node.html.twig
   ▪️ page.html.twig
-->

From documentation page Working With Twig Templates > How to add a page template for a content type .

Solution originally found in How to add page templates for content types in Drupal 8 by Blair Wadman.

A possibly related issue, where Views seems to have lost the ability to detect template suggestions: Views theme hook suggestions in Drupal 10.

Steps to reproduce

Want to add a Twig template for a content type in Drupal 10.3, see a suggestion in the source, add a new template like page--article.html.twig, but it does not take effect ...

Proposed resolution

  1. Clarify the situation for the different versions (Drupal 10.2, 10.3 and 11) so that the documentation can be updated to be correct.
  2. Check the Twig page templates detection status for Views, as a follow up?

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Feature request
Status

Active

Version

11.0 🔥

Component
Theme 

Last updated 1 day ago

Created by

🇩🇰Denmark ressa Copenhagen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @ressa
  • 🇺🇸United States cilefen

    First someone should find the release notes, issue, commit, or change record where the behavior changed.

  • 🇩🇰Denmark ressa Copenhagen

    Yes, that would be the right strategy. I did a quick search in Change Records and didn't find anything, but it may not qualify as that ...

    It would also be nice if someone can verify if the stated situation for Drupal 10.2 / 10.3 / 11 in the Issue Summary is correct.

  • 🇺🇸United States cilefen

    git bisect quickly finds commits.

Production build 0.71.5 2024