' . t('Configure how jQuery behaves on the site. Select which jQuery version, the compression level and whether or not to use a CDN.', array( '@jquery' => 'http://jquery.com', )) . '
'; } } /** * Implements hook_library(). */ function jquery_update_library() { // Register libraries available in the external directory. $path = drupal_get_path('module', 'jquery_update') . '/ui/external'; $libraries['qunit'] = array( 'title' => 'QUnit', 'js' => array( $path . '/qunit.js' => array( 'group' => JS_LIBRARY, 'weight' => 2, ), ), 'css' => array( $path . '/qunit.css' => array(), ), 'version' => '1.11.0', ); $libraries['jquery_update.ajax.fix'] = array( 'title' => 'jQuery Update Version Fix', 'js' => array( drupal_get_path('module', 'jquery_update') . '/js/jquery_update.js' => array( 'group' => JS_LIBRARY, 'weight' => 3, ), ), 'version' => '0.0.1', ); $libraries['jquery.metadata'] = array( 'title' => 'QUnit', 'js' => array( $path . '/jquery.metadata.js' => array( 'group' => JS_LIBRARY, 'weight' => 2, ), ), 'version' => '4187', ); $libraries['jquery.bgiframe'] = array( 'title' => 'bgiframe', 'website' => 'http://docs.jquery.com/Plugins/bgiframe', 'js' => array( $path . '/jquery.bgiframe.js' => array( 'group' => JS_LIBRARY, 'weight' => 2, ), ), 'version' => '2.1.2', ); return $libraries; } /** * Implements hook_library_alter(). * * {@inheritdoc} */ 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); } /** * Implements hook_menu(). */ function jquery_update_menu() { $items['admin/config/development/jquery_update'] = array( 'title' => 'jQuery Update', 'description' => 'Configure settings related to the jQuery upgrade, the library path and compression.', 'page callback' => 'drupal_get_form', 'page arguments' => array('jquery_update_settings_form'), 'access arguments' => array('administer site configuration'), 'file' => 'jquery_update.admin.inc', ); return $items; } /** * Implements hook_form_FORM_ID_alter(). */ function jquery_update_form_system_theme_settings_alter(&$form, $form_state) { // Ignore global theme settings. if (empty($form_state['build_info']['args'][0])) { return; } $form['jquery_update'] = array( '#type' => 'fieldset', '#title' => t('jQuery Update'), '#description' => t('You can optionally select a different version of jQuery to use for pages that are rendered using this theme. This is useful for administrative based themes.'), '#access' => user_access('administer site configuration') && user_access('access administration pages'), ); $form['jquery_update']['jquery_update_jquery_version'] = array( '#type' => 'select', '#title' => t('Theme specific jQuery version'), '#options' => jquery_update_get_version_options(), '#default_value' => theme_get_setting('jquery_update_jquery_version', $form_state['build_info']['args'][0]), ); } /** * Retrieve the jQuery versions available by this module. * * @return array * The available jQuery versions. */ function jquery_update_get_versions() { // Use the advanced drupal_static() pattern, since this has the potential // to be called very often. static $drupal_static_fast; if (!isset($drupal_static_fast)) { $drupal_static_fast['versions'] = &drupal_static(__FUNCTION__, drupal_map_assoc(array( // 1.x. '1.5', '1.6', '1.7', '1.8', '1.9', '1.10', '1.11', '1.12', // 2.x. '2.1', '2.2', // 3.x. '3.1', ))); } return $drupal_static_fast['versions']; } /** * Retrieve the jQuery versions available by this module as select options. * * @param bool $empty * Toggle on whether or not to return an empty option, which will default * to the site wide default setting. * * @return array * The available jQuery versions used to populate a select input. */ function jquery_update_get_version_options($empty = TRUE) { $options = array_merge(array( '' => t('Site default (!version)', array( '!version' => variable_get('jquery_update_jquery_version', '1.10'), )), 'default' => t('1.4 (Drupal core)'), ), jquery_update_get_versions()); if (!$empty) { unset($options['']); } return $options; } /** * Update jQuery to the CDN or local path. * * @param array $javascript * The library definition array as seen in hook_library_alter(). * @param string $cdn * The name of the CDN option to use. Possible options are: * - none * - google * - microsoft * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. * @param string $version * The jQuery version to be used. */ function jquery_update_jquery_replace(&$javascript, $cdn, $path, $min, $version) { // Make sure to use the latest version in given branch. $trueversion = NULL; switch ($version) { case '1.5': $trueversion = '1.5.2'; break; case '1.6': $trueversion = '1.6.4'; break; case '1.7': $trueversion = '1.7.2'; break; case '1.8': $trueversion = '1.8.3'; break; case '1.9': $trueversion = '1.9.1'; break; case '1.10': $trueversion = '1.10.2'; break; case '1.11': $trueversion = '1.11.2'; break; case '1.12': $trueversion = '1.12.4'; break; case '2.1': $trueversion = '2.1.4'; break; case '2.2': $trueversion = '2.2.4'; break; case '3.1': $trueversion = '3.1.1'; break; } $javascript['jquery']['version'] = $trueversion; // Check for CDN support. switch ($cdn) { case 'google': $javascript['jquery']['js']['misc/jquery.js']['data'] = '//ajax.googleapis.com/ajax/libs/jquery/' . $trueversion . '/jquery' . $min . '.js'; $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external'; jquery_update_jquery_backup($javascript, $path, $min, $version); break; case 'microsoft': $javascript['jquery']['js']['misc/jquery.js']['data'] = '//ajax.aspnetcdn.com/ajax/jQuery/jquery-' . $trueversion . $min . '.js'; $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external'; jquery_update_jquery_backup($javascript, $path, $min, $version); break; case 'jquery': $javascript['jquery']['js']['misc/jquery.js']['data'] = '//code.jquery.com/jquery-' . $trueversion . $min . '.js'; $javascript['jquery']['js']['misc/jquery.js']['type'] = 'external'; jquery_update_jquery_backup($javascript, $path, $min, $version); break; case 'none': default: $javascript['jquery']['js']['misc/jquery.js']['data'] = $path . '/replace/jquery/' . $version . '/jquery' . $min . '.js'; break; } } /** * Add the local fallback in case jQuery from the CDN is unavailable. * * @param array $javascript * The $libraries array as seen in hook_library_alter() * @param string $path * The path to the module where replacements can be found. * @param string $min * The '.min' to include in the file name if we are requesting a minified * version. * @param string $version * The jQuery version to be used. */ function jquery_update_jquery_backup(&$javascript, $path, $min, $version) { $javascript['jquery']['js'][] = array( 'data' => 'window.jQuery || document.write("