' . 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_update.browser.fix'] = array( 'title' => 'jQuery Update Browser Fix', 'js' => array( drupal_get_path('module', 'jquery_update') . '/js/jquery_browser.js' => array( 'group' => JS_LIBRARY, 'weight' => 3, ), ), 'version' => '0.0.1', ); $libraries['jquery_update.position.fix'] = array( 'title' => 'jQuery Update Position Fix', 'js' => array( drupal_get_path('module', 'jquery_update') . '/js/jquery_position.js' => array( 'group' => JS_THEME, 'weight' => 2, ), ), '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', JQUERY_UPDATE_DEFAULT_JQUERY_VERSION); // 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']) && !empty($_POST['ajax_page_state']['jquery_version_token'])) { $ajax_version = $_POST['ajax_page_state']['jquery_version']; $token = $_POST['ajax_page_state']['jquery_version_token']; $allowed_versions = array('default') + jquery_update_get_versions(); if (in_array($ajax_version, $allowed_versions) && drupal_valid_token($token, $ajax_version)) { $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, 'jquery_version_token' => drupal_get_token($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); $jqueryui_custom_version = (bool) variable_get('jquery_update_custom_version_jqueryui', FALSE); // Replace jQuery UI with CDN or local files. If from a CDN include all of // jQuery UI. if ($jqueryui_custom_version || version_compare($jquery_version, '1.6', '>=')) { jquery_update_jqueryui_replace($libraries, $cdn, $path, $min); } // Add jquery-cookie plugin. jquery_update_jquery_cookie_replace($libraries, $path, $min); // Add jquery.form plugin. jquery_update_jquery_form_replace($libraries, $path, $min, $jquery_version); // 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 jquery update'), 'file' => 'jquery_update.admin.inc', ); $items['admin/config/development/jquery_update/refresh-version-info'] = array( 'title' => 'Refresh jQuery Update latest version info', 'description' => "Update jQuery Update's info about the latest versions of its jQuery libraries.", 'page callback' => 'jquery_update_refresh_version_info', 'access arguments' => array('administer jquery update'), '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 jquery update'), ); $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.12', // 2.x. '2.2', ))); } 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', JQUERY_UPDATE_DEFAULT_JQUERY_VERSION), )), 'default' => t('1.4 (Drupal core)'), ), jquery_update_get_supported_version_options()); if (!$empty) { unset($options['']); } return $options; } /** * Generate an array of jQuery versions including any custom version. */ function jquery_update_get_supported_version_options() { $options = array(); foreach (jquery_update_get_versions() as $version) { $options[$version] = $version; } $custom_jquery = variable_get('jquery_update_custom_version_jquery', FALSE); if (!empty($custom_jquery)) { $options[$custom_jquery] = $custom_jquery . ' ' . t('(Custom)'); } 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(array &$javascript, $cdn, $path, $min, $version) { $custom_path = variable_get('jquery_update_custom_path_jquery', FALSE); $custom_version = variable_get('jquery_update_custom_version_jquery', FALSE); if (!empty($custom_path) && $version == $custom_version) { $javascript['jquery']['js']['misc/jquery.js']['data'] = $custom_path; $javascript['jquery']['js']['misc/jquery.js']['type'] = url_is_external($custom_path) ? 'external' : 'file'; if (!empty($custom_version)) { $javascript['jquery']['version'] = $custom_version; jquery_update_add_jquery_browser_fix($javascript, $custom_version); jquery_update_add_jquery_position_fix($javascript, $custom_version); } return; } // In case we've been passed an obsolete jQuery version ensure that the // versions stored in settings are all supported, and map the current request // to a supported version. $supported_versions = jquery_update_get_versions(); if (!in_array($version, $supported_versions)) { _jquery_update_convert_settings_to_supported_versions(); // The global default and any theme-specific versions should have been // updated but we still need to replace the version passed to this function. $version = _jquery_update_map_to_supported_version($version); } // Make sure to use the latest version in given branch. $trueversion = NULL; switch ($version) { case '1.12': $trueversion = '1.12.4'; break; case '2.2': $trueversion = '2.2.4'; 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; } jquery_update_add_jquery_browser_fix($javascript, $version); jquery_update_add_jquery_position_fix($javascript, $version); } /** * Add a workaround for deprecated jQuery.browser. */ function jquery_update_add_jquery_browser_fix(&$libraries, $jquery_version) { if (version_compare($jquery_version, '1.9', '>=')) { $libraries['jquery']['dependencies'][] = array( 'jquery_update', 'jquery_update.browser.fix', ); } } /** * Add a workaround for .position() behaviour change. */ function jquery_update_add_jquery_position_fix(&$libraries, $jquery_version) { if (version_compare($jquery_version, '3.3', '>=')) { $libraries['jquery']['dependencies'][] = array( 'jquery_update', 'jquery_update.position.fix', ); } } /** * 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(array &$javascript, $path, $min, $version) { $javascript['jquery']['js'][] = array( 'data' => 'window.jQuery || document.write("