What's the best way (best practice) to load a specific stylesheet for specific View. i managed to do it like this, in the template.php file but i'm thinking this is pretty awkward and there is a better way.

if($variables['theme_hook_suggestions'][0] == 'page__dvd_products') {

drupal_add_css(path_to_theme(). '/css/dvd-product-display-view.css', array('group'=>CSS_THEME));
} else {

drupal_add_css(path_to_theme(). '/css/dvd-product-display.css', array('group'=>CSS_THEME));

what is a better way to do this? i'm still kind of new at this. any feedback would be greatly appreciated. thanks


Looks like you need Code per Views Display module.


I'd avoid installing additional modules when you don't have to. If you have a custom module, you can use hook_views_pre_build(&$view) as in below:

function MYMODULE_views_pre_build(&$view) {
  if ($view->name == 'NAME_OF_VIEW_HERE') { // put the name of the view here
    drupal_add_css(path_to_theme(). '/css/dvd-product-display-view.css', array('group'=> CSS_THEME));

See https://api.drupal.org/api/views/views.api.php/function/hook_views_pre_build/7 for more info on hook_views_prebuild(&$view).

Also, if you use Views PHP (https://drupal.org/project/views_php), you can add a PHP area to the header and add the drupal_add_css() lines there.

  • thanks but i don't know what you mean when referring to additional modules. i was referring to a specific page view.
    – TopTomato
    Jun 4 '14 at 1:50

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.