Search code examples
javascriptdrupaldrupal-viewsfacebook-appsdrupal-templates

Drupal 7 template views preprocess add javascript: what am I doing wrong?


I have read that the best-practice to add a javascript file to a particular view is to use the preprocessor and drupal_add_js. What I'm trying to add is a Facebook app script. I know the script works, because I've tested just adding it in tags right in the header of the view.

What isn't working for me is the code I've added to my theme's template.php file below. As far as I can tell from searching, this should work (obviously replacing THEME_NAME and VIEW_NAME with the actual names), but the js file is just not being added. I also tried just adding it to every page in my theme's .info file, just to see if it would work adding it to every page, but no luck.

function THEME_NAME_preprocess_views_view(&$vars) {
    $view = $vars['view'];

    if ($view->name == 'VIEW_NAME') {
        drupal_add_js(drupal_get_path('theme', 'THEME_NAME') . '/js/facebook.js'); 
    }
}

Solution

  • Just call preprocess for each view. For help visit this link

    function bartik_preprocess_views_view(&$vars) { 
      $function_name = __FUNCTION__ . '__' . $vars['view']->name;
      if (function_exists($function_name)) {
        $function_name($vars);
      }
    }
    
    function bartik_preprocess_views_view__test(&$vars) {
        $view = $vars['view'];
        if ($view->name == 'test') {
            drupal_add_js(drupal_get_path('theme', 'bartik') . '/js/facebook.js');
        }
    }