You are here

function jquery_update_library_alter in jQuery Update 7.3

Same name and namespace in other branches
  1. 7 jquery_update.module \jquery_update_library_alter()
  2. 7.2 jquery_update.module \jquery_update_library_alter()

Implements hook_library_alter().

File

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

Code

function jquery_update_library_alter(&$libraries, $module) {

  // Immediately return if not modifying the system libraries.
  if ($module !== 'system') {
    return;
  }
  $path = drupal_get_path('module', 'jquery_update');
  $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min';
  $jquery_version = variable_get('jquery_update_jquery_version', '1.10');

  // Make sure we inject either the minified or uncompressed version as desired.
  $cdn = variable_get('jquery_update_jquery_cdn', 'none');

  // Replace jQuery with the alternative version.
  $theme_version = theme_get_setting('jquery_update_jquery_version');
  if ($theme_version && version_compare($jquery_version, $theme_version, '!=')) {
    $jquery_version = $theme_version;
  }

  // If the ajax version is set then that one always win.
  if (!empty($_POST['ajax_page_state']['jquery_version'])) {
    $ajax_version = $_POST['ajax_page_state']['jquery_version'];
    if (in_array($ajax_version, array(
      'default',
    ) + jquery_update_get_versions())) {
      $jquery_version = $ajax_version;
    }
  }

  // Always add a new jquery_version array to ajaxPageState.
  // This is what we used to determine which version to use
  // for any ajax callback.
  $libraries['drupal.ajax']['js'][] = array(
    'data' => array(
      'ajaxPageState' => array(
        'jquery_version' => $jquery_version,
      ),
    ),
    'type' => 'setting',
  );
  $libraries['drupal.ajax']['dependencies'][] = array(
    'jquery_update',
    'jquery_update.ajax.fix',
  );

  // Don't replace anything if Drupal provided jQuery should be used
  if ('default' == $jquery_version) {
    return;
  }
  jquery_update_jquery_replace($libraries, $cdn, $path, $min, $jquery_version);

  // Replace jQuery UI with CDN or local files. If from a CDN include all of
  // jQuery UI.
  if (version_compare($jquery_version, '1.6', '>=')) {
    jquery_update_jqueryui_replace($libraries, $cdn, $path, $min);
  }

  // Replace the jQuery Cookie plugin.
  $libraries['cookie']['js']['misc/jquery.cookie.js']['data'] = $path . '/replace/ui/external/jquery.cookie.js';

  // Noting the version based on git commit as no version number is available.
  $libraries['cookie']['version'] = '67fb34f6a866c40d0570';

  // Replace jQuery Form.
  $libraries['jquery.form']['website'] = 'https://github.com/jquery-form/form';
  $jquery_form_versions = array(
    // jQuery Form 4, prior to version 4.2.1, had a serious regression that
    // broke Drupal's AJAX system because it didn't deserialize "+" back into
    // spaces which would cause triggering button values to not match in PHP.
    // @see https://www.drupal.org/node/2860158
    '4.2.1' => '1.7',
    // jQuery Form 3 indicates that it's compatible with jQuery >= 1.5. However,
    // it has parsing issues when used with Drupal and jQuery 1.5.
    // @see https://www.drupal.org/node/2604976
    '3.51.0' => '1.6',
    // Older versions.
    '2.69' => '1.4',
  );
  foreach ($jquery_form_versions as $jquery_form_version => $compatibility) {
    if (version_compare($jquery_version, $compatibility, '>=')) {
      $libraries['jquery.form']['js']['misc/jquery.form.js']['data'] = $path . '/replace/jquery.form/4/jquery.form' . $min . '.js';
      $libraries['jquery.form']['version'] = $jquery_form_version;
      break;
    }
  }

  // Add jQuery.migrate plugin, if needed.
  jquery_update_jquery_migrate_replace($libraries, $path, $min, $jquery_version);
}