10

Im writing a custom module that need to insert some javascript and css files only in a Views page.

Im using hook_preprocess_page, but I can not tell if the current page is from a view:

function mymodule_preprocess_page(&vars)
{
    var_dump($vars); //output: nothings that reference the views!
    if([view page])
    {
        drupal_add_js([...]);
        drupal_add_css([...]);
        // Rebuild scripts 
        $scripts = drupal_get_js();
        $vars['scripts'] = $scripts;
        // Same for css
    }
}

I know i could use a template file (page-my_view_page_path.tpl.php), but the js and extra css must be included only if my module is enabled; so I like to keep this things directly inside my module code.

Ideas?

  • You can use template files in your module. – Jeremy French Mar 3 '11 at 12:05
  • @Jeremy: add your answer explaining how, so people can vote and argue. – Strae Mar 3 '11 at 13:30
  • It wasn't directly related to the answer, just pointing out that a tpl file can be included in a module. a DANieL seemed to be indicating that it couldn't. – Jeremy French Mar 3 '11 at 14:39
  • well.. i could include the theme page.tpl, but my goal is to keep this module totally indipendent from the theme.. – Strae Mar 3 '11 at 14:47
  • You can implement Views' templates pre-processing your module, as suggested in Jeremy French's answer. – Pierre Buyle May 21 '15 at 12:22
12

views_get_page_view() finds out what, if any, page view is currently in use. If it returns NULL, then the current page is not a views' page.

But using drupal_add_js() and drupal_add_css() in a hook_preprocess_page() won't work as expected because the variable $script and $style variables have already been set by template_preprocess_page(). Views's template preprocessing (see Jeremy French's answer) is probably a better place to add your CSS and JavaScript.

  • I updated my answer for you second paragraph - i know is a good practice to add css or js in the theme (theme_preprocess_*), but my need is module dependandt. – Strae Mar 3 '11 at 13:29
  • Excellent, views_get_page_view just gets the already loaded view from cache, so it shouldn't have a performance impact. IMHO this should be the accepted answer. – marcvangend Jun 10 '11 at 8:28
  • @Strae My suggestion was to use HOOK_preprocess_views_view(&$variables) or another Views' template preprocess hook instead of HOOK_preprocess_page(&$variables). This way you can get the views in $variables and use drupal_add_js() and drupal_add_css() safely. – Pierre Buyle Nov 25 '15 at 14:29
11

There is a lengthy thread on preprocessors for views here. This comment looks very simple to me.

function mymodule_theme_registry_alter(&$theme_registry) {
  //dpm($theme_registry);
  $theme_registry['views_view__YOUR_VIEW_NAME_HERE']['preprocess functions'][] = 'mymodule_preprocess_func';
}

// now go on and play with your new preprocess function
function mymodule_preprocess_func(&$vars) {
  // etc
}
  • I agree that preprocessing the output of the View instead of the entire page is probably the more correct approach. – Garrett Albright Mar 28 '11 at 18:30
8

If you're in a hook_preprocess_page() then, by definition, your view has a page display and a menu path, which must be unique - so you can do this:

function mymodule_preprocess_page(&vars)
{
    var_dump($vars); //output: nothings that reference the views!
    if($_GET['q'] == 'my/view/path')
    {
        drupal_add_js([...]);
        drupal_add_css([...]);
    }
}

If you have arguments being passed to this page, so you need parts of $_GET['q'], then do this instead with Drupal's arg() function:

if(arg(0) == 'my' && arg(1) == 'view' && arg(2) == 'path')

http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/arg

  • I would not recommend examining $_GET['q']. Use menu_get_item() or menu_get_object() instead. Neither requires an argument if you are inspecting the current page, and they both cache their results. – Mikey P Mar 3 '11 at 21:31
  • Using drupal_add_js() and drupal_add_css() in a hook_preprocess_page() won't work as expected because the variable $script and $style variables have already been set by template_preprocess_page(). User views_get_page_view() to retrieve the view for the current page (if any). – Pierre Buyle Jun 15 '11 at 10:44
3

For me this one works:

function MYMODULE_preprocess_page(&$vars) {
  $view = (array)views_get_page_view();
  if (!empty($view)) {
    // do stuff
  }
}
2

If you are adding JS or CSS to a specific callback then better to use hook_page_build():

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.