1

In Drupal, every Views page has a class that is stored in $css_name (inside views-view.tpl.php).

Is there are any way to get this class to appear in the body class of the page?

Ideally, I would like to prefix it with page- (So views-index, would become page-views-index). I would also like to do it without install extra modules.

3

Two simple ways I might approach this:

  1. Context Module
  2. jQuery

1) Context Module: create a new context where Condition is "Views" (select the appropriate view) and the Reaction is "Theme HTML" (add the class you want to display in the body there).

Cons: the class is not dynamic, unfortunately. Or at least I have not investigated any way to make dynamically name the class (i.e. page-[node:field_field_yourfieldvalue]). Because of this, you will most likely need to create a new context for each variation if you choose this approach.

2) jQuery: if you want to get the classes in views and add an altered version of these classes to the body tag, then you could use some javascript to pull that off. Something to the effect of this might work:

var myClasses = $(.view-name).attr('class');

$('body').addClass(myClasses);

Something like that, just cleaned up. Hope that helps :)

1

You can do something like that:

function THEMENAME_preprocess_html(&$vars) {
  if ($the_view = views_get_page_view()) {
   $vars['classes_array'][] = 'views-' . $the_view->name;
  }
}

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.