Custom page twig template not working

Created on 24 June 2025, 2 months ago

Problem/Motivation

Custom twig theme for a new content type not being picked up or suggested in Twig debugging

Steps to reproduce

1. Create a new content type "testing"
2. Create a node in this content type
3. In custom subtheme of Bootstrap5, create /templates/layout/page--node--testing.html.twig and add customisations
4. Clear cache
5. View the node
6. Enable twig debugging and view file name suggestions

Expected result = Custom template is used for this node.

Actual result = page.html.twig is used and theme debugging doesn't show the suggestion

I added the following hook to the .theme file but this didn't work:
function mytheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if (!empty($variables['node']) && $variables['node'] instanceof \Drupal\node\NodeInterface) {
$suggestions[] = 'page__node__' . $variables['node']->getType();
}
}

Proposed resolution

How do I enable custom twig templates for content types?

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇦🇺Australia Spanners Sydney

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

Comments & Activities

  • Issue created by @Spanners
  • 🇦🇺Australia Spanners Sydney

    Managed for fix my issue by adding the following to my .theme file:

     use Drupal\node\NodeInterface;
    
    /**
     * Implements hook_theme_suggestions_page_alter().
     */
    function mytheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
      // Try accessing node directly from the route
      $node_from_route = \Drupal::routeMatch()->getParameter('node');
      if ($node_from_route instanceof NodeInterface) {
        \Drupal::logger('csbb')->notice('Node found from route match: @type', [
          '@type' => $node_from_route->getType(),
        ]);
    
        $suggestions[] = 'page__node__' . $node_from_route->getType();
      }
    }
    
  • Status changed to Closed: works as designed 7 days ago
  • 🇦🇺Australia jannakha Brisbane!
Production build 0.71.5 2024