I have a textfield that uses autocomplete. It basically works with the default misc/autocomplete.js. Now I want to override this with my own file. So I have in modules/custom/myModule/myModule.info.yml:

libraries-override:
  core/drupal.autocomplete:
    js:
      misc/autocomplete.js: js/autocomplete.js

with having my autocomplete.js in modules/custom/myModule/js. However, this is ignored. The page still uses core/misc/autocomplete.js. I tried several variants of the override entry, but nothing works. Any idea?

Comments

Joe Keene’s picture

libraries-override:
  core/drupal.autocomplete:
    js:
      misc/autocomplete.js: js/autocomplete.js

This is correct and assumes the root of the theme folder. But issue was that I was using 'classy' as my base theme, in order to get the override working I had to put the override in the 'classy.info.yml' which isn't advisable from core. So I moved it to the contrib themes folder and changed it's name and now my override works fine - hopefully this will help someone else...

SalvadorP’s picture

I'm using a custom theme and a custom module, and the libraries-override solution didn't work for me. 

I tried to override it on the custom theme, and on the custom module, but with no luck.

What worked for me was this:

  • Copy the core/misc/autocomplete.js file to custom_module/js/my_autocomplete.js
  • Declare in the custom_module/custom_module.libraries.yml file a new library:
my_autocomplete:
  version: 0.1
  js:
    js/my_autocomplete.js: {}
  dependencies:
    - core/jquery
    - core/drupalSettings
  • Modify the my_autocomplete.js file and add the settings parameter to the attach:
attach: function attach(context, settings) {
  • And finally on the form or on the element attach the library with the settings.
    In my case, I added it to the element:
'#attached' => [
  'library' => ['my_autocomplete/my_autocomplete'],
  'drupalSettings' => [],
],

The reason to add drupalSettings is to provide the possibility to send variables to the autocomplete file.
For instance to modify some of the options of the autocomplete.