0

In Drupal 8, is there a way to get html.html.twig to only show on a content type like html--blog.html.twig? The suggestions in the debug don't seem to give me that option.

<!-- FILE NAME SUGGESTIONS:
   * html--node--1222.html.twig
   * html--node--%.html.twig
   * html--node.html.twig
   x html.html.twig
-->
1
  • 1
    You would have to add the template suggestion yourself via hook_preprocess_html and some logic
    – Kevin
    Oct 14, 2016 at 2:10

1 Answer 1

1

You can do this with a theme suggestions alter hook:

/**
 * Implements THEME_theme_suggestions_html_alter().
 */
function MYTHEME_theme_suggestions_html_alter(&$suggestions, $vars, $hook) {

  // Check to see if you are on a node.
  if ($node = \Drupal::routeMatch()->getParameter('node')) {

    // Get the node type.
    $type = $node->getType();

    // Create the theme hook suggestions.
    $suggestions[] = 'html__' . $type;
    $suggestions[] = 'html__node_' . $type;
  }
}

Of course, change "MYTHEME to your theme name. I tested this on a blog content type for example and the new template suggestions printed out after creating this hook and clearing cache.

<!-- THEME HOOK: 'html' -->
<!-- FILE NAME SUGGESTIONS:
   * html--node-blog.html.twig
   * html--blog.html.twig
   * html--node--15.html.twig
   * html--node--%.html.twig
   * html--node.html.twig
   x html.html.twig
-->

The new theme hooks here are:

  * html--node-blog.html.twig
  * html--blog.html.twig

The content type name will change accordingly in the naming suggestion.

To test, I copied html.html.twig and named as html--node-blog.html.twig and it worked great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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