Drupal – Use a different jQuery version for the frontend and backend


Jan 4th 2013 in explication

On Drupal frontend I want the latest jQuery especially because bootstrap require at least jQuery 1.7. Drupal 7 come with jQuery 1.4 and it’s not going to be updated.

The module jQuery Update take care of updating jQuery to a newer version. I even use the dev version so I can have jQuery 1.8. Sadly this creates some bugs in the admin. To avoid conflict I decided to load a different jQuery version for the backend and the frontend.

To do that you have to create a very small module with this code:


/**
* Implements hook_module_implements_alter().
*/
function mymodule_module_implements_alter(&$implementations, $hook) {
if ($hook == 'library_alter') {
if(isset($implementations['jquery_update'])) {
// Move jquery update to the end. This will make sure our hook_library_alter
// is always called before the jquery_update.
$jquery_update = $implementations['jquery_update'];
unset($implementations['jquery_update']);
$implementations['jquery_update'] = $jquery_update;
}
}
}

/**
* Implements hook_library_alter().
*/
function mymodule_library_alter(&$libraries, $module) {
// If it is the admin theme all we want to do is change the global $conf
// variable so when jquery_update runs right after us it will use 1.5.
// We are not using path_is_admin(current_path()) because some admin path can use
// the frontend theme like node edit page
global $theme_key;
if (variable_get('admin_theme') == $theme_key) {
// Modifying global $conf variable, can be dangerous. Be carefull.
global $conf;
$conf['jquery_update_jquery_version'] = '1.5';
}
}

I also write some patches to try to solve the compatibility problem in jQuery and in core but I prefer to stay safe for now until it’s fixed. If you want to know more about the issue check out http://drupal.org/node/1498858.