You are here

function jquery_update_preprocess_page in jQuery Update 6

Same name and namespace in other branches
  1. 6.2 jquery_update.module \jquery_update_preprocess_page()

Implementation of moduleName_preprocess_hook().

Replace Drupal core's jquery.js with the new one from jQuery Update module.

1 string reference to 'jquery_update_preprocess_page'
jquery_update_theme_registry_alter in ./jquery_update.module
Implementation of hook_theme_registry_alter().

File

./jquery_update.module, line 46
Updates Drupal to use the latest version of jQuery.

Code

function jquery_update_preprocess_page(&$variables) {

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

    // Perform the logic if either jQuery Update's jquery.js is newer than
    // core's, or if we're using a different compression type.
    if (variable_get('jquery_update_replace', TRUE) || variable_get('jquery_update_compression_type', 'pack') != 'pack') {

      // Get an array of all the JavaScript files loaded by Drupal on this page.
      $scripts = drupal_add_js();

      // Replace jquery.js first.
      $new_jquery = array(
        jquery_update_jquery_path() => $scripts['core']['misc/jquery.js'],
      );
      $scripts['core'] = array_merge($new_jquery, $scripts['core']);
      unset($scripts['core']['misc/jquery.js']);

      // Loop through each of the required replacements.
      foreach (jquery_update_get_replacements() as $type => $replacements) {
        foreach ($replacements as $find => $replace) {

          // If the file to replace is loaded on this page...
          if (isset($scripts[$type][$find])) {

            // Create a new entry for the replacement file, and unset the original one.
            $replace = JQUERY_UPDATE_REPLACE_PATH . '/' . $replace;
            $scripts[$type][$replace] = $scripts[$type][$find];
            unset($scripts[$type][$find]);
          }
        }
      }
      $variables['scripts'] = drupal_get_js('header', $scripts);
    }
  }
}