4

At moment Drupal add css class page-VIEWPATH to the body tag. How can I add css class page-VIEWNAME to the body tag without third party module or JQuery?

Thank you in advance!

  • I had look at that post before, didn't find the answer I was looking for. I can't simply add $variables['classes_array'][] = 'new-class'; for the page that isn't the view page. – wzh Jul 27 '15 at 5:16
  • you can add a condition if (current_path() == 'my-path') { $variables['classes_array'][] = 'new-class'; } – Andrew Jul 27 '15 at 5:21
6

It turns out, this can be done with a combination of hook_preprocess_html and function views_get_page_view to get a true <body> class for a given view and display (see the API page).

So wrapping it up within the hook_preprocess_html context, it would look something like this:

/**
 * Implements template_preprocess_html().
 *
 * Misc page preprocess functions.
 */
function mytheme_preprocess_html(&$vars) {

// Query the view and add a class
$view = views_get_page_view();
 if (
  isset($view) &&
  $view->name == 'actions' &&
  $view->current_display == "page"
) {
    $vars['classes_array'][] = 'views-actions-page';
  }
}

So above, I query the view name and display and the key is $view = views_get_page_view();. This works great for custom Views body classes per view and display.

The rendered HTML result is something like this:

<body class="html not-front logged-in no-sidebars 
page-actions section-actions page-views flush-header views-actions-page">

** views-actions-page is the class that we added with the function above.

It should be noted that the advantage of this vs. using views generated classes are that if the URL of the page changes, your custom Views class will not whereas page-actions section-actions would change based on the URL. So using those classes could be unreliable if they are used for theming as it would have a negative ripple effect.

  • Thank you very much. It solved my problem. The note you described is exactly the problem I was facing. Thanks again! – wzh Nov 3 '15 at 8:27
  • @wzh - Welcome, glad that helped! – Danny Englander Nov 3 '15 at 16:09
3

Use the following function in template.php of your theme folder

function THEME_preprocess_views_view(&$variables){
    $view = $variables['view'];
    if($view->name == "YOUR_VIEW_NAME" && $view->current_display == "MACHINE_NAME_OF_VIEW"){
        $variables['classes_array'][]="page-".$view->name;
    }
}

you can also set some other conditions like current_path or anything else you want.

  • I tried something very similar earlier but it didn't work, just tried yours too and it didn't add the view name as class. Does $variables['classes_array'][] work on body class in preprocess_views_view? Because that is what I tried before. Thanks for reply. – wzh Jul 27 '15 at 5:49
  • You must clear the Drupal cache. I tried it on localhost before answering – M a m a D Jul 27 '15 at 5:51
  • This is very odd, I did clear the cache every time. Just for testing I got rid of IF statement and that is not working neither. – wzh Jul 27 '15 at 5:59
  • Print some text at the beginning of the function to make sure it get called and executed – M a m a D Jul 27 '15 at 6:00
  • It executed and print the text. – wzh Jul 27 '15 at 6:06

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.