5

Is there any way to make a custom page.html.twig template in Drupal 8?

By default, Drupal generates some suggestions, but I'm looking for something like page--node--content-type.html.twig.

1
  • I think the question and especially the title is very confusing, but I think that's because you don't know the correct name for what you are asking. I'm not sure if you added the tag, but I guess it was @kiamlaluno: What you are searching for are theme hook suggestions, writing an answer now..
    – Berdir
    Commented Dec 21, 2014 at 21:51

5 Answers 5

14

You are looking for hook_theme_suggestions_HOOK_alter().

HOOK is the name of the template, in your case, page. You should be able to add the following to your theme in the .theme file.

In there, do something like this:

function yourtheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $suggestions[] = 'page__node__' . $node->getType();
  }
}
8
  • i'm sorry but didn't work. Commented Dec 23, 2014 at 21:06
  • i have found one solution (workaround) may be not the best but at list it works. if i add these lines "if ($node = \Drupal::routeMatch()->getParameter('node')) { $suggestions[] = $prefix . $delimiter . $node->getType(); }" to core theme.inc file in 'theme_get_suggestions' than it will work and even generates comments on debug mode! how can i add to theme file to make it work. Commented Dec 24, 2014 at 8:12
  • 1
    I noticed that I didn't change HOOK to page in my example, if you didn't change that then that would explain why it didn't work. If you did, post your code, otherwise we can't help.
    – Berdir
    Commented Dec 27, 2014 at 15:56
  • hook_theme_suggestions_HOOK() is not invoked for themes: "This hook allows the module implementing hook_theme() for a theme hook to provide alternative theme function or template name suggestions. This hook is only invoked for the first module implementing hook_theme() for a theme hook." Other than that, fantastic answer. I made the necessary edits.
    – star-szr
    Commented Feb 21, 2015 at 1:45
  • @Cottser: You are half-correct :) It is not called for themes, correct (which IMHO is a bug, like a few other hooks that themes should be able to implement), but it is called for a all modules: github.com/drupal/drupal/blob/8.0.x/core/lib/Drupal/Core/Theme/…, thats a normal hook invocation, for all modules. So the documentation is wrong. But thanks for the edit!
    – Berdir
    Commented Feb 21, 2015 at 9:52
6

This code from Jeff Burnz (https://www.drupal.org/node/2521876#comment-10684366) solved it for me. It also places the suggestion above page--node so the capability to override with page--node--% and page--node--[id] remains.

<?php
function THEME_NAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  // Add content type suggestions.
  if ($node = \Drupal::request()->attributes->get('node')) {
    array_splice($suggestions, 1, 0, 'page__' . $node->getType());
  }
}
?>
2
  • \Drupal::request()->attributes->get('node') and \Drupal::routeMatch()->getParameter('node') both return the node object, I'm not sure if one is quicker or more recommended than the other.
    – Jeff Burnz
    Commented Jan 26, 2016 at 0:53
  • And heres the answer: drupal.org/node/2295317 reading the patch all requests for the node object were converted to routeMatch :)
    – Jeff Burnz
    Commented Jan 26, 2016 at 2:00
3

Your file name is bad, rename page.htm.twig to page.html.twig

After for clear your cache during page refresh, enable an use settings.local.php file inside your installation, on linux by example, copy and rename the example file to the good location with this command:

cp /sites/example.settings.local.php /sites/default/settings.local.php

Uncomment this lines inside your settings.php :

 if (file_exists(__DIR__ . '/settings.local.php')) {
     include __DIR__ . '/settings.local.php';
 }

And inside /sites/default/services.yml use this value:

debug: true

Inside settings.local.php, uncomment this line :

$settings['cache']['bins']['render'] = 'cache.backend.null';

Because Drupal say

Do not use this setting until after the site is installed.

Now, your environment is ready, you can create theme and modules, but if you add a template file, you need to clear the cache manually :)

Note: don't use this configuration in production (bad performances for user/server).

2
  • The OP wants to use page--node--[content-type].html.twig instead of page.html-twig Drupal 8 uses.
    – avpaderno
    Commented Dec 27, 2014 at 17:57
  • Regarding the Twig settings in services.yml, auto_reload is already enabled when you enable debug, and I have yet to see any value in disabling the Twig cache. Disabling the Twig cache makes it harder to debug compiled templates and I have not seen any benefits so far in enabling it. I would say the vast majority of folks will do great by only enabling debug.
    – star-szr
    Commented Feb 21, 2015 at 1:38
1

it is ok ,according to drupal 8 theme api the template file for page is page.html.twig, may be you did not clear cache.

2
  • The OP wants to create a custom template file for a page basing on the content type of the node being shown. The question is not "Why isn't Drupal 8 using page.html.twig?"
    – avpaderno
    Commented Dec 21, 2014 at 11:11
  • 1
    drupal 8 default suggestions are: * page--node--2.html.twig * page--node--%.html.twig * page--node.html.twig * page.html.twig but i would like to make custom page.html.twig for example page--node--article.html.twig where article is content(node) type name. if i just add this file to my theme it won't work. this page--node--article.html.twig or this page--article.html.twig doesn't work. Commented Dec 21, 2014 at 16:51
-1

It's like:

  • node--[content-type].html.twig ex: node--article.html.twig
  • node--[content-type]--[display-format].html.twig, ex: node--article--full.html.twig

Here the screenshot of template suggestion:

enter image description here

2
  • it's not a page.html.twig template. Commented Mar 22, 2015 at 19:11
  • The question was about page templates. Commented Jan 23, 2016 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.