0

How can i add a class with 'html' tag(not with body) for some specified pages (10 nodeID's) ? I tried with html preprocess, but didn't worked,

2 Answers 2

0

You can add template_preprocess_html to your module and add the classes via $variables['html_attributes']['class'].

Since this is html.html.twig's preprocessor, it has no notion of what a Node is. It could also be rendering a non-node page (e.g. the user login). Some modules may add Node information to the variables, but there is no guarantee. To be safe, you'll have to check if the current route is a node and then check the IDs afterward.

function your_module_preprocess_html(&$variables) {
  if ($is_current_rout_is_a_node) {
    if ($is_this_the_right_node) {
      $variables['html_attributes']['class'][] = 'your class';
    }
  }
}
0

You can override html.html.twig in your theme as html--node--[NID].html.twig, and add the class into the HTML in that template.

<html{{ html_attributes.addAttribute("my attribute") }}>
0

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.