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
- Clarify the situation for the different versions (Drupal 10.2, 10.3 and 11) so that the documentation can be updated to be correct.
- 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