Step 1: Find the library name in core that the JS or CSS file to override belongs to.
For example, it is clear that core\modules\file\file.js
belongs to the file
module. Review at core/modules/file/file.libraries.yml
to find the library name . In the yaml file, the library name is the top-level item in the yaml structure where file.js
is defined. In this case drupal.file
.
#core/modules/file/file.libraries.yml
drupal.file:
version: VERSION
js:
file.js: {} #### Drupal 8+ defines the js/css to override in libraries.yml
dependencies:
- core/jquery
- core/jquery.once
- core/drupal
- core/drupalSettings
Step 2: Find the fully-qualified library name.
The fully-qualified library name is generally prefixed by the module name, in the format <modulename>/<libraryname>
. However, some core system libraries don't follow this format, so if unsure of the fully qualified library name, search for instances of the <libraryname>
in the core codebase, which may turn up a string with the fully-qualified library name like drupal/<libraryname>
.
In this case the fully qualified name is file/drupal.file
.
Step 3: Override the file in your theme
In your custom theme's info.yml file add the following lines:
libraries-override:
file/drupal.file:
js:
file.js: js/file.js
If you want to override a core or contrib CSS file, the syntax is similar:
libraries-override:
filter/caption:
css:
theme: # <-- this line must match the definition in libraries.yml
css/filter.caption.css: css/components/captions.css
# ^ ^
# | |
# File path as defined File path relative to your
# in *.libraries.yml theme's folder.
Step 4: Copy the file into your theme, and modify it to your needs
For the 2 examples given above, you'd place the files.js inside a folder js
in your theme, and you'd rename filter.caption.css to captions.css and place it inside the css/components
folder of your theme.
Step 5: Clear caches and reload the page.
Use your Web browser's resource inspector to verify the path to the overridden file is coming from your theme folder.
delete Drupal.behaviors.filePreviewLinks.attach; Drupal.behaviors.filePreviewLinks.detach;
in your own library loaded afterwards.