29

I have a view in my site that has a specific class name. I want to know, inside the template.php file of a theme, how I can know if a view with specific class name is available in the requested page.

This is very important for me because I need to include specific JavaScript and CSS, when a view with a specific class name (such as image-gallery) is used in a page.

5 Answers 5

44

You could use the template_preprocess_views_view() hook to do this:

function THEME_preprocess_views_view(&$vars) {
  $view = &$vars['view'];
  // Make sure it's the correct view
  if ($view->name === 'your-view-name') {
    // add needed javascript
    drupal_add_js(drupal_get_path('theme', 'your-theme') . '/your-js.js');
    // add needed stylesheet
    drupal_add_css(drupal_get_path('theme', 'your-theme') . '/your-css.css');
  }
}

Note that you could also check the specific display of the view using:

if ($view->name == 'your-view-name' && $view->current_display === 'your-display-id') {
  // include javascript & css here
}

You should be able to check for a custom css class like this:

if ($view->name == 'your-view-name' && $view->display[$view->current_display]->display_options['css_class'] === 'your-css-class') {
   // include javascript & css here
}
5
  • Thanks a lot, but this function recognize the view by name, I want to recognize the class name, because There are several views in one page that has same class name . for example 2 views has 'gallery-image' class name & 3 views has 'light-box' class name. I want to add js for example if a view with 'light-box' css class name is available in the page.
    – Mehrdad201
    Commented Apr 24, 2012 at 8:28
  • Can't you just check the display? if($view->name == 'your-view-name' && $view->display_id == 'your-display-id') Commented Apr 24, 2012 at 9:10
  • no, because it is not clear that how many views are available in the page. maybe there ate 10 views that all of them have a same css class name.
    – Mehrdad201
    Commented Apr 24, 2012 at 9:59
  • I implemented this hook and flushed all caches - but it didn't get called. Not sure why. Yes I double-checked the name.
    – jdhildeb
    Commented Apr 11, 2016 at 21:48
  • That seems very strange? You have replaced THEME with the name of your actual theme? This should work just fine as long as you implement it correctly. Commented Apr 12, 2016 at 12:50
9

You can use views_get_page_view function to find out which view is being served on the page. It return views object. Note that in the code snippet below you should compare with views machine readable name, but of course you can write your own comparaison basing on the view object returned by views_get_page_view

function YOURTHEMENAME_preprocess_page(&$variables) {
    $view = views_get_page_view();
    if($view->name == 'YOURVIEW') {
    //Do what ever you want 
    }
6

Just in case this is useful to anyone else stumbling across this question, searching like I did for attach JavaScript to a Drupal View. In terms of D7 & Views 3.7, the following worked best for me:

function HOOK_views_pre_render ( &$view ) { 
  /// check to make sure the view has a classname
  if ( $view->display_handler && !empty($view->display_handler->options['css_class']) ) {
    $cln = $view->display_handler->options['css_class'];
    $cls = 'CLASS GOES HERE';
    /// test that the classname contains our class
    if ( preg_match('/(^|\s+)' . preg_quote($cls) . '(\s+|$)/i', $cln) ) {
      /// build the path to the js, which is local to my module, js/view.js
      $sep = DIRECTORY_SEPARATOR;
      $dir = rtrim(drupal_get_path('module', 'HOOK'), $sep);
      $pth = "{$dir}{$sep}js{$sep}view.js";
      drupal_add_js($pth);
    }
  }
}

This was beneficial as I wanted to keep the code within my module, rather than the theme — because the enhancements brought by the JavaScript had nothing to do with visual appearance.

NOTE: Obviously HOOK should be replaced with your module name, in both locations, and CLASS GOES HERE should also be replaced with the class you are searching for.

3
  • For clarity, why was pre_render better than pre_process?
    – Nick
    Commented Oct 21, 2014 at 20:02
  • @Nick ~ There isn't a HOOK_views_pre_process (api.drupal.org/api/views/views.api.php/7) unless you are meaning the reason why to use this over THEME_preprocess_views_view -- in which case it's only out of preference of where you wish to add your code modifications. HOOK_views_pre_render can be hooked into from a module, whereas THEME_preprocess_views_view should go in your theme/template file. You could also use HOOK_views_post_render if you prefer.
    – Pebbl
    Commented Oct 21, 2014 at 23:43
  • Sorry, I did mean THEME_preprocess_views_view, yes... I'm sure I have done hook_preprocess_views_view in modules before.
    – Nick
    Commented Oct 23, 2014 at 12:54
4

If you're on drupal 6 or 7 you can also include javascript assets with the module context add assets. Which does require context but you'll rest assure that it'll always be included when the view is being rendered.

https://drupal.org/project/context_addassets

From the module's project page:

Have you ever wanted to include javascript or css when a particular view or block is being rendered? Or have you ever wanted to include javascript or css for just the front page without having to write code?

Context add assets allows you to do this. It has an easy to use UI to allow you to do this all without writing any code. Because it uses ctools all of this is also exportable.

2

Write JavaScript code that searches for specific class in the view, using jQuery's hasClass(), and include your JavaScript file when the condition is satisfied.

Another way is to put a template for your view, and add JavaScript code through that template using drupal_add_js().

2
  • Can you tell me, how can I do this. Indeed how can include css and js files after a conditional expression in java script is true ??!
    – Mehrdad201
    Commented Apr 24, 2012 at 8:29
  • For Drupal 8, this answer outlines how to make a custom view template for the view output to do this: stackoverflow.com/a/43131240/227926 Commented Oct 17, 2018 at 20:49

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.