0

Is it possible to add the view mode used for a display into the body class?

At the moment, the view mode is being inserted into one of the region classes, but I would like to add it also to the body class. This would allow me to change the page to full width depending on the view mode.

I am using the Bootstrap theme.

1
  • Are you talking about view mode of any content type? Commented Mar 27, 2018 at 21:57

1 Answer 1

0

If you are using sub theme of bootstrap theme then you can possibly add class to body tag of the node.

Go to SubThemeDirectory/and open template.php and add this hook_preprocess_html()

function THEMENAME_preprocess_html(&$vars) {
  if ($node = menu_get_object()) {
    $vars['body_attributes_array']['class'][] = 'mode-' . $vars['page']['content']['system_main']['nodes'][$node->nid]['#view_mode']; // Node ID class
  }
}

I hope this'll at least give an idea how to add class attribute.

5
  • It doesn't matter what theme you are using, you should be able to add a body class in any theme.
    – Kevin
    Commented Mar 27, 2018 at 22:38
  • Thank you very much for your answer. This looks good, but how can I get $node ($node->nid) from inside the function? If I know this I can get it to work.
    – Ben
    Commented Mar 28, 2018 at 9:17
  • @Ben, I already added if ($node = menu_get_object()), it'll load all objects into $node so you can access any object of the node like $node->nid. See more about menu_get_object() Commented Mar 28, 2018 at 11:58
  • @usmanjutt84 - Yes, you are absolutely right. I was trying to access $node from above your part of the code. Thanks very much for your help.
    – Ben
    Commented Mar 29, 2018 at 10:00
  • I'm glad it works for you Commented Mar 29, 2018 at 10:23

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.