Libraries are defined in YAML format, of course. You can define one or more libraries in each file.
Libraries are also used for CSS but we’ll ignore that for this discussion
Define a Library
Create or edit the
Libraries are defined in YAML format, of course. You can define one or more libraries in each file.
Libraries are also used for CSS but we’ll ignore that for this discussion
Create or edit the
some-file-name can be any name really, but should be the name of your theme or module.
Drupal will aggregate JS files by default, so to avoid that if necessary:
You can optionally provide a version number per library, but Drupal doesn’t use it for anything:
You can also define dependencies per library, which are followed:
by default loads JS into the footer; to load to the header:
Reference them in your my-theme-name.info.yml file. They load globally as in on every page.
Here we’re loading our libraries this-lib-name and core/jquery (which is no longer loaded by default).
You can attach a JS library from a twig template. It will only load if the template loads which means you can conditionally load it based on the naming structure of the Twig template file.
PHP allows you to control what libraries are loaded and when.
Of course you can load any number of libraries using this, and related, hooks.
If not loading everywhere by using the .yml file.
See also related THEME_preprocess_HOOK()’s. Although some discourage using these hooks as they’re intended for preprocessing variables.
To support Drupal caching, if you want to *conditionally* attach libraries, you need to provide cacheability metadata*.
*Metadata is comprised of a combination of up to 3 things: tags, contexts, and max-age. See the Cache API
External “libraries” still have to be defined inside Drupal libraries*.
Since Drupal will minify, if it already is, let know:
If attributes are needed in the resulting line:
To add “computed” settings or configuration, first you have to define a dependency on core/drupalSettings.
Then access the some-prop setting in JS with:
Almost always JavaScript should be included in a library.
But if you must, put the complete script tag in the html.html.twig file (yes, “html” is repeated twice.)
Use this when you want to dynamically specify JS (or CSS) Libraries used across multiple requests.
hook_library_info_alter(&$libraries, $extension)
Allows modules and themes to change libraries' definitions.
An example of this is the Color module which uses this hook to replace CSS Libraries with the “colored” CSS libraries.
Use it to programmatically add an entire library definition. And they are still cached.
Modules may implement hook_library_info_build() to add dynamic library definitions
While you’re suppose to use libraries you could attach into HTML head and create a tag directly. These are (re)built on every single request and therefore aren’t cached and can slow down Drupal, so beware.
If possible, it’s best to leave your JS file(s) static and just use JS Settings configurations.