Score:4

How do I override a single JavaScript file used in a library?

zw flag

I am trying to override the Search API autocomplete JavaScript file. In mytheme.libraries, I added the following lines.

mytheme_autocomplete:
  js:
    'assets/js/autocomplete.js': { }

In mytheme.info, I added the following lines.

libraries-override:
  search_api_autocomplete/search_api_autocomplete:
    js:
      component:
        js/search_api_autocomplete.js: mytheme_autocomplete

When I visit the site, js/search_api_autocomplete.js is still loaded, while assets/js/autocomplete.js isn't loaded.

How do I override a single JavaScript file used in a library?

Score:8
in flag

If you're overriding a single js file, you need to specify a file, not a library:

libraries-override:
  search_api_autocomplete/search_api_autocomplete:
    js:
      js/search_api_autocomplete.js: assets/js/autocomplete.js

However, specify your theme's library if you intend to override an entire library:

libraries-override:
  search_api_autocomplete/search_api_autocomplete: mytheme/mytheme_autocomplete

See the Overriding and extending libraries documentation on d.o

May  avatar
zw flag
Thanks, I was able to override the whole library using your 2nd example, but could not override the single JS file in your first example/
sonfd avatar
in flag
I believe you may have added the extra "component" layer in the overrides definition. That should not be there as the search_api_autocomplete library does not specific the javascript that way. I updated the example with something that should work with that file.
sonfd avatar
in flag
Incidentally, I believe the component / theme / base, etc weights only apply to css files, not js files, i.e. js would never have component in its definition hierarchy. See https://www.drupal.org/node/2274843#library
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.