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.

  • Are you talking about view mode of any content type? – usmanjutt84 Mar 27 '18 at 21:57
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.

  • It doesn't matter what theme you are using, you should be able to add a body class in any theme. – Kevin Mar 27 '18 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 Mar 28 '18 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() – usmanjutt84 Mar 28 '18 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 Mar 29 '18 at 10:00
  • I'm glad it works for you – usmanjutt84 Mar 29 '18 at 10:23

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.