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.

up vote 40 down vote accepted

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
}
  • 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 Apr 24 '12 at 8:28
  • Can't you just check the display? if($view->name == 'your-view-name' && $view->display_id == 'your-display-id') – Cyclonecode Apr 24 '12 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 Apr 24 '12 at 9:59
  • I just got a downvote on this answer, don't understand why? I think it would be nice if downvoters could leave a comment stating why they don't think a specific answer is good/correct. – Cyclonecode Apr 8 '16 at 9:09
  • 1
    @Cyclone Sorry, It works. I was wrong to write the name of the views. – Daniele Sep 27 '16 at 14:24

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 
    }

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.

  • For clarity, why was pre_render better than pre_process? – Nick Oct 21 '14 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 Oct 21 '14 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 Oct 23 '14 at 12:54

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.

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().

  • 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 Apr 24 '12 at 8:29

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.