jQuery version per theme

By berliner, 23 November, 2013

Recently I came across something that feels like a bug in the jQuery Update module. The current version allows to specify a modern jQuery version to use for a site, which is awesome when you want to include modern user interface functionality using jQuery UI. Additionally the module allows to override the jQuery version that should be used, but only for admin pages (introduced after a rather long issue discussion).

At a first glance this seems like a nice solution. Lots of websites that I'm working on use one or more frontend themes with heavy usage of modern versions of jQuery UI, and a backend theme (often simply Seven) for administrative usage. The admin override allows to revert the jQuery version to one that is compatible with the backend theme.

The problems comes when you look how that is defined. For a page to be considered an admin page it is sufficient for any custom or contrib module to implement hook_admin_paths(). Whether pages that are defined admin pages are rendered using the backend theme is a totally different question. A very good example from core is the node module:

 * Implements hook_admin_paths().
function node_admin_paths() {
  if (variable_get('node_admin_theme')) {
    $paths = array(
      'node/*/edit' => TRUE,
      'node/*/delete' => TRUE,
      'node/*/revisions' => TRUE,
      'node/*/revisions/*/revert' => TRUE,
      'node/*/revisions/*/delete' => TRUE,
      'node/add' => TRUE,
      'node/add/*' => TRUE,
    return $paths;

It provides support for a setting that is configurable under /admin/appearance and that controls whether certain node edit pages should be rendered using the admin theme. While this should in general work, it just doesn't. I didn't really dig too much into this, because the general idea of this solution seems like a misconception to me.

The version of jQuery that is used on a given page, is completely theme related. The theme doesn't care to know if it displays an admin page, neither does jquery_update. All that jquery_update needs to know, is which version of jQuery is compatible with the theme that displays the current page. So effectively, the jQuery version becomes a theme setting. If the road is chosen to allow different versions for different themes, then this separation should be clear.

Things get a lot easier from then on. And it opens up a lot more possibilities. Even 3 or 4 different themes, each one using a distinctive jQuery version, become possible. No need to content yourself with only one backend and one frontend theme. Obviously I haven't been the only one who ran into this and I was glad to provide help in this issue. There is a patch that applies to the latest jquery_update-7.x-2.x-dev version (as of the time of this writing) and that enables theme specific settings for jQuery.

If you want this functionality too, got to the issue and leave a comment: https://drupal.org/node/1969244