Drupal8: Add class to body tag

Here is the solution for adding our custom class to body tag using preprocess_html() function.

function hook_preprocess_html(&$variables) {
$variables[‘attributes’][‘class’][] = ‘class-name’;
}

If you want to add class for all node of specify content type, you can do like below

function hook_preprocess_html(&$variables) {
if ($node = \Drupal::request()->attributes->get(‘node’)) {
if($node->getType() == ‘content_type_name’) {
$variables[‘attributes’][‘class’][] = ‘class-name’;
}
}
}

Hope this will helpful to you…

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: