1

How can I stop jQuery being loaded in Drupal 8, even when modules which use jQuery are on the page?

I have an existing styleguide which I'm pulling into a Drupal 8 site. I already have all the javascript including jQuery as part of the styleguide and I want to keep this separate from Drupal.

For anonymous users everything works fine. However admin users have access to the admin toolbar. This loads in Drupal's own version of jQuery which conflicts with the styleguide's and causes the admin toolbar to break.

I tried adding this to my theme's libraries file:

core/jquery: false
core/jquery.once: false
core/jqueryui: false 

Inspecting the page it did appear to block some jQuery references but not all of them:

enter image description here

However when I remove my entries from my theme's libraries file I can see that in fact all of the same jQuery files are being loaded, so I don't actually think it achieves anything.

enter image description here

I know that normally one would not load in jQuery themselves and just use Drupal's version, but the styleguide requires the latest version and is also consumed by other applications so I cant change it very much.

migrated from stackoverflow.com May 31 '16 at 20:31

This question came from our site for professional and enthusiast programmers.

3

You should be able to alter the JavaScript files include on the page with an implementation of hook_js_alter().

1

In THEMENAME.theme:

<?php

function THEMENAME_js_alter(&$javascript, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {
  $javascript['core/assets/vendor/jquery/jquery.min.js']['data'] = '';
  $javascript['core/assets/vendor/jquery.ui/ui/core-min.js']['data'] = '';
  $javascript['core/assets/vendor/jquery.ui/ui/widget-min.js']['data'] = '';
  $javascript['core/assets/vendor/jquery-form/jquery.form.min.js']['data'] = '';
  $javascript['core/assets/vendor/jquery.cookie/jquery.cookie.min.js']['data'] = '';
  $javascript['core/assets/vendor/jquery.ui/ui/mouse-min.js']['data'] = '';
  $javascript['core/assets/vendor/jquery.ui/ui/draggable-min.js']['data'] = '';
  $javascript['core/assets/vendor/jquery.ui/ui/resizable-min.js']['data'] = '';
  $javascript['core/assets/vendor/jquery.ui/ui/dialog-min.js']['data'] = '';
  $javascript['core/misc/dialog/dialog.jquery-ui.js']['data'] = '';
  $javascript['core/assets/vendor/jquery-joyride/jquery.joyride-2.1.min.js']['data'] = '';
}
  • Note that is an hard solution. A cleaner solution would be to properly upgrade the jQuery budnle with Drupal (as done by the JQuery Update module) and have the style guide integration to force it own. – Pierre Buyle Jun 2 '16 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.