0

The objective of this is to remove all legacy javascript files from the homepage except for the new javascript files I specifically want to load.

Here is how I remove all the Javascript files I don't need:

function phptemplate_preprocess_page(&$vars) {
    $template_files = $vars['template_files'];
    $scripts = drupal_add_js();

    if (in_array('page-front', $template_files === true) {
        // Here I unset all the javascripts that I don't want to be loaded in the homepage
        unset($scripts['theme']['themes/"theme_name"/js/jquery.jcarousel.min.js']);
        ...
        // Once cleaned up all unncessary javascripts, I load them into $var['scripts']
        $var['scripts'] = drupal_get_js('header', $scripts);
    }
}

This works fine, however it only works when the jQuery Update module is desactivated. If the jQuery Update module is active, somehow it overseeds this logic and all the javascript files are loaded anyways.

I don't want to use the jQuery Update logic in the homepage, but I'm affraid that if I disable this module, I can provoke negative side effects in the other pages of the website.

Conclusion: I want to desactivate jQuery Update logic for handling javascript files but only for the homepage.

Any other alternative solution / conclusion to this problem is also welcome.

1 Answer 1

1

The reason why your theme_preprocess_page function isn't working, is because the jQuery update module uses a clever implementation of hook_theme_registry_alter to ensure it's implementation of hook_preprocess_page is called last.

What I recommend you do is create a new module and implement your own hook_theme_registry_alter and hook_preprocess_page functions. See a simple example below which has taken some code snippets from the jquery update module:

function mymodule_theme_registry_alter(&$theme_registry) {

  if (isset($theme_registry['page'])) {
    if (count($theme_registry['page']['preprocess functions']) > 0) {          
      if ($key = array_search('mymodule_preprocess_page', $theme_registry['page']['preprocess functions'])) {
        unset($theme_registry['page']['preprocess functions'][$key]);
      }
    }

    // Now tack it on at the end so it runs after everything else.
    $theme_registry['page']['preprocess functions'][] = 'mymodule_preprocess_page';
  }
}

This hook implementation tells Drupal to invoke our custom hook_preprocess_page function after all other modules have run their hooks.

Then we need to define our own custom implementation of hook_preprocess_page which will alter the scripts included on the front page:

function mymodule_preprocess_page(&$vars) {

  // Only do this for pages that have JavaScript on them.
  if (!empty($vars['scripts'])) {

    $template_files = $vars['template_files'];
    $scripts = drupal_add_js();

    if (in_array('page-front', $template_files === TRUE)) {
      // Here I unset all the javascripts that I don't want to be loaded in the homepage
      unset($scripts['theme']['themes/"theme_name"/js/jquery.jcarousel.min.js']);
      // Once cleaned up all unncessary javascripts, I load them into $var['scripts']
      $vars['scripts'] = drupal_get_js('header', $scripts);
    }
  }
}

Using this approach in your own custom module will make sure that any alterations you want to make to your javascript includes will happen very last.

Good luck! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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