Skip to content

Commit ad206aa

Browse files
committed
Issue #1969244 by berliner, markcarver, mongolito404: Specify jQuery version per theme
1 parent 1ad9a08 commit ad206aa

File tree

2 files changed

+199
-32
lines changed

2 files changed

+199
-32
lines changed

jquery_update.install

+54-1
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,38 @@ function jquery_update_requirements($phase) {
2525
return $requirements;
2626
}
2727

28+
/**
29+
* Helper function for setting a theme jQuery version during install or update.
30+
*
31+
* @param string $theme_key
32+
* The machine name of the theme to set.
33+
* @param string $version
34+
* The MAJOR.MINOR jQuery version to set.
35+
*/
36+
function _jquery_update_set_theme_version($theme_key, $version) {
37+
// Retrieve the cached theme settings.
38+
theme_get_setting('jquery_update_jquery_version', $theme_key);
39+
$theme_settings = drupal_static('theme_get_setting', array());
40+
41+
// Set the jQuery version.
42+
$theme_settings[$theme_key]['jquery_update_jquery_version'] = $version;
43+
variable_set('theme_' . $theme_key . '_settings', $theme_settings[$theme_key]);
44+
}
45+
46+
/**
47+
* Implements hook_install().
48+
*/
49+
function jquery_update_install() {
50+
// Use core's default jQuery version for the "seven" admin theme.
51+
_jquery_update_set_theme_version('seven', 'default');
52+
}
53+
2854
/**
2955
* Implements hook_uninstall().
3056
*/
3157
function jquery_update_uninstall() {
3258
variable_del('jquery_update_compression_type');
3359
variable_del('jquery_update_jquery_version');
34-
variable_del('jquery_update_jquery_admin_version');
3560
variable_del('jquery_update_jquery_cdn');
3661
}
3762

@@ -47,3 +72,31 @@ function jquery_update_update_7000() {
4772
// Restore the default version of jQuery.
4873
variable_del('jquery_update_jquery_version');
4974
}
75+
76+
/**
77+
* Convert jquery_update_jquery_admin_version to an admin theme setting.
78+
*/
79+
function jquery_update_update_7001() {
80+
// Detect if the previous feature of the "admin version" variable is set.
81+
// @see https://www.drupal.org/node/1969244
82+
$admin_theme = variable_get('admin_theme', FALSE);
83+
$admin_version = variable_get('jquery_update_jquery_admin_version', 'default');
84+
85+
// Ensure that if "seven" is set as the admin theme and no "admin version"
86+
// is present, the version used on the admin theme is the "default" core
87+
// provides to ensure major compatibility with contrib modules.
88+
if (!$admin_version && $admin_theme === 'seven') {
89+
$admin_version = 'default';
90+
}
91+
// Skip this update if the "admin version" was never set and the admin theme
92+
// is not set as "seven".
93+
elseif (!$admin_version) {
94+
return;
95+
}
96+
97+
// Continue setting the admin theme jQuery version.
98+
_jquery_update_set_theme_version($admin_theme, $admin_version);
99+
100+
// Remove the admin version variable.
101+
variable_del('jquery_update_jquery_admin_version');
102+
}

jquery_update.module

+145-31
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,14 @@ function jquery_update_library_alter(&$javascript, $module) {
8787
$cdn = variable_get('jquery_update_jquery_cdn', 'none');
8888

8989
// Replace jQuery with the alternative version.
90-
$admin_version = variable_get('jquery_update_jquery_admin_version', '');
91-
92-
if (!empty($admin_version) && path_is_admin(current_path())) {
93-
if (version_compare($version, $admin_version, '!=')) {
94-
$version = $admin_version;
95-
}
90+
$theme_version = theme_get_setting('jquery_update_jquery_version');
91+
if ($theme_version && version_compare($version, $theme_version, '!=')) {
92+
$version = $theme_version;
9693
}
9794
// If the ajax version is set then that one always win.
9895
if (!empty($_POST['ajax_page_state']['jquery_version'])) {
9996
$ajax_version = $_POST['ajax_page_state']['jquery_version'];
100-
if (in_array($ajax_version, array('default', '1.5', '1.6', '1.7', '1.8', '1.9', '1.10'))) {
97+
if ($ajax_version == 'default' || in_array($ajax_version, jquery_update_get_versions())) {
10198
$version = $ajax_version;
10299
}
103100
}
@@ -172,34 +169,89 @@ function jquery_update_settings_form() {
172169
'#title' => t('Version options'),
173170
);
174171

172+
$default_version = variable_get('jquery_update_jquery_version', '1.10');
173+
$version_options = jquery_update_get_version_options(FALSE);
175174
$form['version_options']['jquery_update_jquery_version'] = array(
176175
'#type' => 'select',
177-
'#title' => t('Default jQuery Version'),
178-
'#options' => array(
179-
'default' => t('Default (provided by Drupal)'),
180-
'1.5' => '1.5',
181-
'1.7' => '1.7',
182-
'1.8' => '1.8',
183-
'1.9' => '1.9',
184-
'1.10' => '1.10',
185-
),
186-
'#default_value' => variable_get('jquery_update_jquery_version', '1.10'),
187-
'#description' => t('Select which jQuery version to use by default.'),
176+
'#title' => t('Site default jQuery version'),
177+
'#options' => $version_options,
178+
'#default_value' => $default_version,
179+
'#description' => t('Select which version of jQuery to use on the site.'),
188180
);
189181

190-
$form['version_options']['jquery_update_jquery_admin_version'] = array(
191-
'#type' => 'select',
192-
'#title' => t('Alternate jQuery version for administrative pages'),
193-
'#options' => array(
194-
'' => t('Default jQuery Version'),
195-
'default' => t('Default (provided by Drupal)'),
196-
'1.5' => '1.5',
197-
'1.7' => '1.7',
198-
'1.8' => '1.8',
199-
'1.10' => '1.10',
200-
),
201-
'#default_value' => variable_get('jquery_update_jquery_admin_version', ''),
202-
'#description' => t('Optionally select a different version of jQuery to use on administrative pages.'),
182+
$themes = list_themes();
183+
$theme_default = variable_get('theme_default', FALSE);
184+
$admin_theme = variable_get('admin_theme', FALSE);
185+
$header = array(t('Theme'), t('jQuery version'), t('Operations'));
186+
$rows = array();
187+
$themes_collapsed = TRUE;
188+
// Go through all themes.
189+
foreach ($themes as $theme_key => $theme) {
190+
// Skip disabled themes, but only if they are not configured as admin
191+
// theme. This is an inconsistency in drupal core, that you can select a
192+
// disabled theme as admin theme.
193+
if (!$theme->status && $theme_key !== $admin_theme) {
194+
continue;
195+
}
196+
197+
// Retrieve the version jQuery for this theme.
198+
$theme_version = theme_get_setting('jquery_update_jquery_version', $theme_key);
199+
// Do not collapse the fieldset if a theme has set a jQuery version.
200+
if ($theme_version) {
201+
$themes_collapsed = FALSE;
202+
}
203+
204+
// Replace or modify the version name to be displayed.
205+
if (empty($theme_version)) {
206+
$theme_version = t('Site Default');
207+
}
208+
elseif (in_array($theme_version, array_keys($version_options))) {
209+
$theme_version = $version_options[$theme_version];
210+
}
211+
else {
212+
$theme_version .= ' (' . t('unknown version') . ')';
213+
}
214+
215+
// Provide additional information for default and admin themes.
216+
$theme_name = $theme->info['name'];
217+
if ($theme_key === $theme_default && ($theme_key === $admin_theme || empty($admin_theme))) {
218+
$theme_name .= ' (' . t('default/admin theme') . ')';
219+
}
220+
elseif ($theme_key === $theme_default) {
221+
$theme_name .= ' (' . t('default theme') . ')';
222+
}
223+
elseif ($theme_key === $admin_theme) {
224+
$theme_name .= ' (' . t('admin theme') . ')';
225+
}
226+
227+
// Construct the table row.
228+
$rows[] = array(
229+
$theme_name,
230+
$theme_version,
231+
l(t('Configure'), 'admin/appearance/settings/' . $theme_key, array(
232+
'attributes' => array(
233+
'class' => array(
234+
'module-link',
235+
'module-link-configure',
236+
),
237+
),
238+
'query' => drupal_get_destination(),
239+
'fragment' => 'edit-jquery-update',
240+
)),
241+
);
242+
}
243+
244+
$form['version_options']['themes'] = array(
245+
'#type' => 'fieldset',
246+
'#title' => t('Theme specific versions'),
247+
'#description' => t('You can override the default jQuery version above on each themes settings page. This is useful for administrative based themes.'),
248+
'#collapsible' => TRUE,
249+
'#collapsed' => $themes_collapsed,
250+
);
251+
$form['version_options']['themes']['overrides'] = array(
252+
'#theme' => 'table',
253+
'#header' => $header,
254+
'#rows' => $rows,
203255
);
204256

205257
$form['jquery_update_compression_type'] = array(
@@ -239,6 +291,68 @@ function jquery_update_settings_form() {
239291
return system_settings_form($form);
240292
}
241293

294+
/**
295+
* Implements hook_form_FORM_ID_alter().
296+
*/
297+
function jquery_update_form_system_theme_settings_alter(&$form, $form_state) {
298+
// Ignore global theme settings.
299+
if (empty($form_state['build_info']['args'][0])) {
300+
return;
301+
}
302+
$form['jquery_update'] = array(
303+
'#type' => 'fieldset',
304+
'#title' => t('jQuery Update'),
305+
'#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.'),
306+
);
307+
$form['jquery_update']['jquery_update_jquery_version'] = array(
308+
'#type' => 'select',
309+
'#title' => t('Theme specific jQuery version'),
310+
'#options' => jquery_update_get_version_options(),
311+
'#default_value' => theme_get_setting('jquery_update_jquery_version', $form_state['build_info']['args'][0]),
312+
);
313+
}
314+
315+
/**
316+
* Retrieve the jQuery versions available by this module.
317+
*
318+
* @return array
319+
* The available jQuery versions.
320+
*/
321+
function jquery_update_get_versions() {
322+
// Use the advanced drupal_static() pattern, since this has the potential
323+
// to be called very often.
324+
static $drupal_static_fast;
325+
if (!isset($drupal_static_fast)) {
326+
$drupal_static_fast['versions'] = &drupal_static(__FUNCTION__, drupal_map_assoc(array(
327+
'1.5', '1.7', '1.8', '1.9', '1.10',
328+
)));
329+
}
330+
return $drupal_static_fast['versions'];
331+
}
332+
333+
/**
334+
* Retrieve the jQuery versions available by this module as select options.
335+
*
336+
* @param bool $empty
337+
* Toggle on whether or not to return an empty option, which will default
338+
* to the site wide default setting.
339+
*
340+
* @return array
341+
* The available jQuery versions used to populate a select input.
342+
*/
343+
function jquery_update_get_version_options($empty = TRUE) {
344+
$options = array_merge(array(
345+
'' => t('Site default (!version)', array(
346+
'!version' => variable_get('jquery_update_jquery_version', '1.10'),
347+
)),
348+
'default' => t('1.4 (Drupal core)'),
349+
), jquery_update_get_versions());
350+
if (!$empty) {
351+
unset($options['']);
352+
}
353+
return $options;
354+
}
355+
242356
/**
243357
* Update jQuery to the CDN or local path.
244358
*

0 commit comments

Comments
 (0)