2

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

I have an existing style guide which I'm pulling into a Drupal 8 site. I already have all the JavaScript including jQuery as part of the style guide 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 style guide'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 style guide requires the latest version and is also consumed by other applications so I cant change it very much.

2 Answers 2

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'] = '';
}
1
  • 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. Commented Jun 2, 2016 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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