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.
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.
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();
}
}
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.
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());
}
}
?>
\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.
Commented
Jan 26, 2016 at 0:53
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).
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
.
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.
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: