logo

Harnessing the Power of libraries in Drupal Theming: A Comprehensive Guide

Posted by Tajinder Minhas - July 21, 2023
drupal libraries

Drupal theming is an art that allows developers to create visually appealing and feature-rich websites. To achieve customizations and extend the default functionality, Drupal provides the 'libraries-override' feature, which empowers themers to replace, remove, and extend libraries and assets. In this article, we will explore all the conditions of 'libraries-override' and provide a complete code example of creating a custom theme library using 'custom_theme.libraries.yml' and implementing it on specific pages.

  1. Replacing an Entire Library

The first condition of 'libraries-override' allows themers to replace an entire library with their custom version. This is particularly useful when you want to swap out the default library for one that better aligns with your theme's requirements. Here's an example:

yaml code

libraries-override: core/drupal.collapse: mytheme/collapse

In the above code, we are replacing the 'core/drupal.collapse' library with 'mytheme/collapse.' This ensures that the custom library is loaded instead of the default one.

  1. Replacing an Asset with Another

With 'libraries-override,' themers can also replace specific assets within a library. This enables you to modify the appearance and behavior of elements without replacing the entire library. Here's an example:

yaml code

libraries-override: subtheme/library: css: theme: css/layout.css: css/my-layout.css

In this code, we are replacing 'css/layout.css' from the 'subtheme/library' with 'css/my-layout.css,' providing a tailored styling to the theme.

  1. Removing an Asset

Sometimes, you may want to eliminate certain assets from a library to optimize your theme or avoid conflicts. The 'libraries-override' feature can help you achieve this by setting the asset to 'false.' For instance:

yaml code

libraries-override: drupal/dialog: css: theme: dialog.theme.css: false

In this example, we remove the 'dialog.theme.css' asset from the 'drupal/dialog' library, preventing it from being loaded.

  1. Removing an Entire Library

If you find that a particular core library is unnecessary for your theme, you can remove it entirely using 'libraries-override.' This can help reduce unnecessary dependencies and improve performance. Here's an example:

yaml code

libraries-override: core/modern: false

In this code, we remove the 'core/modern' library from the theme.

  1. Extending the Drupal User Library and Including Assets from base theme

Drupal themers can also extend existing libraries and include additional assets. This is achieved using the 'libraries-extend' directive. Let's see how to extend 'core/drupal.user' and include assets from 'classy.libraries.yml':

yaml code

libraries-extend: core/drupal.user: - classy/user1 - classy/user2

With this code, we extend 'core/drupal.user' and include the 'user1' and 'user2' assets from 'classy.libraries.yml.'

Complete Code Example: Creating and Using a Custom Theme Library

Now, let's go through a step-by-step example of creating a custom theme library using 'custom_theme.libraries.yml' and applying it to specific pages:

Step 1: Create 'custom_theme.libraries.yml' File

Inside your theme's directory, create a 'custom_theme.libraries.yml' file. Define your custom library and its assets. For instance:

yaml code

my_custom_library: version: 1.x css: theme: css/custom-style.css: {} js: js/custom-script.js: {}

In this example, we've defined a custom library called 'my_custom_library' that includes 'custom-style.css' and 'custom-script.js.'

Step 2: Include the Custom Library in 'custom_theme.info.yml'

In the 'custom_theme.info.yml' file, include your custom library:

yamlcode

libraries-override: core/drupal.user: - my_custom_library

By adding this line, your 'my_custom_library' will be loaded whenever the 'core/drupal.user' library is called.

Step 3: Apply the Custom Library to Specific Pages

To apply the custom library to specific pages, create a theme preprocess function in your theme's 'mytheme.theme' file (for Drupal 8 and later):

For Drupal 8 and later:

php code

function mytheme_preprocess_page(&$variables) { // Add custom library to specific pages. if ($variables['node']->getType() == 'article') { $variables['#attached']['library'][] = 'mytheme/my_custom_library'; } }

In this example, we are applying the 'my_custom_library' to all pages with the content type 'article.' You can modify the conditions according to your specific use case.