Score:-1

Cannot make page--node--nid to work

se flag

For a node whose content type is Page and whose ID is 112, I created the page--node-112.html.twig template file, but Drupal is still using the page--page.html.twig template file.

How can I make Drupal use the page--node-112.html.twig template file?

Kevin avatar
in flag
Is it even a valid template suggestion? Did you clear cache?
Score:1
us flag

None of the Drupal core modules and themes suggests 'page__' . $bundle. If that suggestion is added by a module (or a theme) after the suggestions added by system_theme_suggestions_page(), which is the hook that suggests 'page__node__' . $nid, that suggestion is picked up first, since suggestions are checked from the last to the first.

The code that checks for which of the suggestions there is a template file is in ThemeManager::render().

  // Check if each suggestion exists in the theme registry, and if so,
  // use it instead of the base hook. For example, a function may use
  // '#theme' => 'node', but a module can add 'node__article' as a suggestion
  // via hook_theme_suggestions_HOOK_alter(), enabling a theme to have
  // an alternate template file for article nodes.
  foreach (array_reverse($suggestions) as $suggestion) {
    if ($theme_registry->has($suggestion)) {
      $info = $theme_registry->get($suggestion);
      break;
    }
  }

I assume you are really using a page--node--112.html.twig template file. page--node-112.html.twig isn't a template file Drupal core suggests and it won't be pick up.

Score:0
in flag

Enable Twig Debugging, that will add comments including template file names that can be used to override less specific templates.

The comments look something like this and you can view them in source or your browser's inspector.

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'page' -->
<!-- FILE NAME SUGGESTIONS:
   * page--node--6178.html.twig
   * page--node--%.html.twig
   * page--node--article.html.twig
   * page--node.html.twig
   x page.html.twig
-->

In the above example you can see my page is being rendered using page.html.twig but I have alternatives above that which could be used specific to my content type (article) or node a single id.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.