This post is mostly based on the live-stream session from WebWash: https://www.youtube.com/live/IYuQh1zRwz0. So, if you have the time to watch this genius’s video, definitely check it out instead - you will learn much more compared to reading this post.
1. Asset Injector Module (Not Recommended)
If all you need is some customization using CSS and JS without modifying the actual Drupal theme, you may use the Asset Injector (asset_injector) module (see: https://www.drupal.org/project/asset_injector). Notice that at the very bottom of each injector, you get to select the condition where the injection would occur, for instance, when on a page of content type “Standard page”.
If you would turn off the CSS/JS Aggregator
feature in developers settings, you will be able to find the CSS/JS is in fact getting saved as a file directly inside the asset_injector
module, and being attached to the page:
2. Drupal Theme Library
You may declare CSS and JS assets via your Drupal theme’s theme_name.libraries.yml
file. Below are a few examples from the bootstrap_barrio.libraries.yml
for your reference:
|
|
|
|
As a practical example, let’s say if you would like to use the slick
slider in your project, you can include it in your sub-theme’s library via:
|
|
(*"external:true
" is used to denote that the script is from an external source, and Drupal will not process this script through its asset optimization system. This means the file will NOT be included in the minification, aggregation, or other preprocessing steps.)
|
|
Note that once you have declared the library, it is NOT the end. In order to attach it to pages during rendering, you will either have to:
- Attach it as a global library of the sub-theme via the
theme_name.info.yml
file - Attach it in the relevant Twig template via the
{{attach_library('theme_name/slick-init')}}
function - Attach it in the preprocessing hook via the
theme_name.theme
file
2.a. Attach Library as Global Library
To attach a library to a theme globally, meaning it will get executed on any page using this theme, simply add it to the library
attribute in the theme’s theme_name.info.yml
after declaring the theme as previously instructed in the theme_name.libraries.yml
file:
|
|
2.b. Attach Library via attach_library
function in Twig Template
Adding CSS/JS assets globally may sometimes be overkill. For our example, the slick slider library will only need to be attached whenever a page has such a component, for instance, the front page. Having to load all libraries throughout the website might cause performance issues.
In order to create a slick slider, you might already have component created in drupal backend via paragraph (say “banner
”), and is overriding its template via theme_name/template/paragraph/paragraph--banner.html.twig
.
Then you may attach the library like the following:
|
|
This way the library is only getting attached to the page when this template gets rendered.
(Read more on: https://www.drupal.org/node/2456753)
2.c. Attach Library via preprocess
Hook
If you would like finer control over when the library gets attached, for instance, only attach a library whenever a page “is a front page” OR “has a title being Hello-World”, then adding a preprocess
hook in the theme_name.theme
file will be your go-to solution:
|
|
Below is an example:
(* I also noticed that sometimes the preprocess hook will not get called when the relevant Twig template is not in your theme. Under this condition, copying the *.html.twig
template from the original theme to your sub-theme will resolve it (and I don’t understand why yet…)
(With the preprocess hook, you can also do powerful things such as “add meta description and title” programmatically. See this post: https://www.drupal.org/docs/develop/theming-drupal/add-meta-description-and-title-programmatically for more details)
3. Library Override / Extend
If you wish to extend or override a part of the installed theme’s library (usually the base theme), you can do that via library-extend
or library-override
in your sub-theme’s theme_name.info.yml
file.
If you wish to extend the popper library in your base theme’s library to add an additional JavaScript file for some extra interactivity, you may do that via the following:
|
|
(see: https://youtu.be/IYuQh1zRwz0?t=4461)
Similarly, if you wish to override the typography in your base theme’s library, as well as to disable the component stylesheet for your form, you may do that via the following:
|
|
(see: https://youtu.be/IYuQh1zRwz0?t=4769)
4. Single Directory Component
You may declare an SDC by creating a folder in your theme’s component
directory with the following files:
|
|
(* More details regarding the setup of SDC can be found in one of my previous posts titled: “Single Directory Component with Drupal”, as well as the official guide: https://www.drupal.org/docs/develop/theming-drupal/using-single-directory-components/quickstart)
Once set up, you will be able to use it using the {{include}}
or {{embed}}
keyword in your Twig template, for instance:
|
|
|
|