diff --git a/jquery_update.install b/jquery_update.install index 98da3b7..b5a9204 100644 --- a/jquery_update.install +++ b/jquery_update.install @@ -26,12 +26,37 @@ function jquery_update_requirements($phase) { } /** + * Helper function for setting a theme jQuery version during install or update. + * + * @param string $theme_key + * The machine name of the theme to set. + * @param string $version + * The MAJOR.MINOR jQuery version to set. + */ +function _jquery_update_set_theme_version($theme_key, $version) { + // Retrieve the cached theme settings. + theme_get_setting('jquery_update_jquery_version', $theme_key); + $theme_settings = drupal_static('theme_get_setting', array()); + + // Set the jQuery version. + $theme_settings[$theme_key]['jquery_update_jquery_version'] = $version; + variable_set('theme_' . $theme_key . '_settings', $theme_settings[$theme_key]); +} + +/** + * Implements hook_install(). + */ +function jquery_update_install() { + // Use core's default jQuery version for the "seven" admin theme. + _jquery_update_set_theme_version('seven', 'default'); +} + +/** * Implements hook_uninstall(). */ function jquery_update_uninstall() { variable_del('jquery_update_compression_type'); variable_del('jquery_update_jquery_version'); - variable_del('jquery_update_jquery_admin_version'); variable_del('jquery_update_jquery_cdn'); } @@ -49,19 +74,29 @@ function jquery_update_update_7000() { } /** - * Keep a previously configured jquery version for the admin theme. - * @see https://www.drupal.org/node/1969244 + * Convert jquery_update_jquery_admin_version to an admin theme setting. */ function jquery_update_update_7001() { - $admin_theme_key = variable_get('admin_theme', FALSE); - $admin_version = variable_get('jquery_update_jquery_admin_version', FALSE); - if (!$admin_theme_key || !$admin_version) { - return; + // Detect if the previous feature of the "admin version" variable is set. + // @see https://www.drupal.org/node/1969244 + $admin_theme = variable_get('admin_theme', FALSE); + $admin_version = variable_get('jquery_update_jquery_admin_version', 'default'); + + // Ensure that if "seven" is set as the admin theme and no "admin version" + // is present, the version used on the admin theme is the "default" core + // provides to ensure major compatibility with contrib modules. + if (!$admin_version && $admin_theme === 'seven') { + $admin_version = 'default'; } - $theme_settings = variable_get('theme_' . $admin_theme_key . '_settings', FALSE); - if (!isset($theme_settings['jquery_update_jquery_version'])) { - $theme_settings['jquery_update_jquery_version'] = $admin_version; - variable_set('theme_' . $admin_theme_key . '_settings', $theme_settings); - variable_del('jquery_update_jquery_admin_version'); + // Skip this update if the "admin version" was never set and the admin theme + // is not set as "seven". + elseif (!$admin_version) { + return; } + + // Continue setting the admin theme jQuery version. + _jquery_update_set_theme_version($admin_theme, $admin_version); + + // Remove the admin version variable. + variable_del('jquery_update_jquery_admin_version'); } diff --git a/jquery_update.module b/jquery_update.module index 18d31fd..1738a7e 100644 --- a/jquery_update.module +++ b/jquery_update.module @@ -167,47 +167,76 @@ function jquery_update_settings_form() { $form['version_options'] = array( '#type' => 'fieldset', '#title' => t('Version options'), - '#description' => t('You can override the jQuery version on a per-theme basis on each themes settings page.'), ); $default_version = variable_get('jquery_update_jquery_version', '1.10'); + $version_options = jquery_update_get_version_options(FALSE); $form['version_options']['jquery_update_jquery_version'] = array( '#type' => 'select', - '#title' => t('Default jQuery Version'), - '#options' => array_merge(array('default' => t('Default (provided by Drupal)')), jquery_update_get_versions()), + '#title' => t('Site default jQuery version'), + '#options' => $version_options, '#default_value' => $default_version, - '#description' => t('Select which jQuery version to use by default.'), + '#description' => t('Select which version of jQuery to use on the site.'), ); $themes = list_themes(); - $header = array(t('Theme'), t('Status'), t('jQuery version'), ''); + $theme_default = variable_get('theme_default', FALSE); + $admin_theme = variable_get('admin_theme', FALSE); + $header = array(t('Theme'), t('jQuery version'), t('Operations')); $rows = array(); $themes_collapsed = TRUE; // Go through all themes. - foreach ($themes as $theme_name => $theme) { + foreach ($themes as $theme_key => $theme) { // Skip disabled themes, but only if they are not configured as admin - // theme. This is an inconsitency in drupal core, that you can select a + // theme. This is an inconsistency in drupal core, that you can select a // disabled theme as admin theme. - if (!$theme->status && variable_get('admin_theme', FALSE) != $theme_name) { + if (!$theme->status && $theme_key !== $admin_theme) { continue; } + // Retrieve the version jQuery for this theme. - $theme_version = theme_get_setting('jquery_update_jquery_version', $theme_name); + $theme_version = theme_get_setting('jquery_update_jquery_version', $theme_key); + // Do not collapse the fieldset if a theme has set a jQuery version. + if ($theme_version) { + $themes_collapsed = FALSE; + } + + // Replace or modify the version name to be displayed. if (empty($theme_version)) { - $theme_version = $default_version; + $theme_version = t('Site Default'); } - // Decide whether the override table should be shown. - if ($theme_version != $default_version) { - $themes_collapsed = FALSE; + elseif (in_array($theme_version, array_keys($version_options))) { + $theme_version = $version_options[$theme_version]; + } + else { + $theme_version .= ' (' . t('unknown version') . ')'; } - $uri = 'admin/appearance/settings/' . $theme_name; + + // Provide additional information for default and admin themes. + $theme_name = $theme->info['name']; + if ($theme_key === $theme_default && ($theme_key === $admin_theme || empty($admin_theme))) { + $theme_name .= ' (' . t('default/admin theme') . ')'; + } + elseif ($theme_key === $theme_default) { + $theme_name .= ' (' . t('default theme') . ')'; + } + elseif ($theme_key === $admin_theme) { + $theme_name .= ' (' . t('admin theme') . ')'; + } + + // Construct the table row. $rows[] = array( - $theme->info['name'], - $theme_version != $default_version ? t('Overridden') : t('Default'), + $theme_name, $theme_version, - l(t('Change'), $uri, array( - 'fragment' => 'edit-jquery-update-version', + l(t('Configure'), 'admin/appearance/settings/' . $theme_key, array( + 'attributes' => array( + 'class' => array( + 'module-link', + 'module-link-configure', + ), + ), 'query' => drupal_get_destination(), + 'fragment' => 'edit-jquery-update-version', )), ); } @@ -215,6 +244,7 @@ function jquery_update_settings_form() { $form['version_options']['themes'] = array( '#type' => 'fieldset', '#title' => t('Theme specific versions'), + '#description' => t('You can override the default jQuery version above on each themes settings page. This is useful for administrative based themes.'), '#collapsible' => TRUE, '#collapsed' => $themes_collapsed, ); @@ -265,41 +295,62 @@ function jquery_update_settings_form() { * Implements hook_form_FORM_ID_alter(). */ function jquery_update_form_system_theme_settings_alter(&$form, $form_state) { - if ($form['var']['#value'] == 'theme_settings') { - // global theme settings page + // Ignore global theme settings. + if (empty($form_state['build_info']['args'][0])) { return; } - $theme_key = $form_state['build_info']['args'][0]; - - $default_version = variable_get('jquery_update_jquery_version', '1.10'); - $theme_version = theme_get_setting('jquery_update_jquery_version', $theme_key); - $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.'), ); $form['jquery_update']['jquery_update_jquery_version'] = array( '#type' => 'select', '#title' => t('Theme specific jQuery version'), - '#options' => array( - '' => t('Site wide default (!version)', array('!version' => $default_version)), - ) + jquery_update_get_versions(), - '#default_value' => $theme_version, - '#description' => t('Optionally select a different version of jQuery to use for pages that are rendered using this theme.'), + '#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 + * @return array + * The available jQuery versions. */ function jquery_update_get_versions() { - $versions = &drupal_static(__FUNCTION__); - if (!$versions) { - $versions = drupal_map_assoc(array('1.5', '1.7', '1.8', '1.9', '1.10')); + // 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.5', '1.7', '1.8', '1.9', '1.10', + ))); + } + 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 $versions; + return $options; } /**