Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using Drupal 8 and Bootstrap sub-theme.

I see that classes are being added to my divs in the page template file using the following:

<section{{ content_attributes.addClass(content_classes) }}>

That's great. But what if I wanted to add additional classes to these div tags. How would I do that...in my .theme file somehow?

share|improve this question
up vote 2 down vote accepted

OPTION 1

In your template file, you could find the set content_classes and add another class name to the list.

{%
  set content_classes = [
    'class-name',
    'class-name-other',
  ]
%}
<section{{ content_attributes.addClass(content_classes) }}>

OPTION 2

In your .theme file

function themename_preprocess_html(&$variables) {
  $variables['content_attributes']['class'][] = 'class-name-other';
}

Note: This preprocess is for the html.html.twig file, you need to use the correct preprocess depending on your template file name.

If it's the node.html.twig then use function themename_preprocess_node(&$variables)

If it's the page.html.twig then use function themename_preprocess_page(&$variables)

share|improve this answer
    
In option 2, once this new class gets added in array, then we need to add that class to that particular div or element in .tpl.php Or twig file, right? This I am asking just for my understanding/knowledge. Thanks. – CodeNext 1 hour ago
1  
@CodeNext yes that is correct, you would do <div {{ content_attributes }}> ...... </div> or some element <p {{ content_attributes }}> ...... </p>, <body {{ content_attributes }}> ...... </body> – No Sssweat 1 hour ago
    
Thanks for information... – CodeNext 1 hour ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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