Creating a library in .libraries.ymlfor Drupal 8 , 9 , 10 , and 11

Last updated :  

In Drupal module development, efficiently managing your CSS and JavaScript assets is crucial to ensure consistent styling and behavior across your modules. The .libraries.yml system in Drupal provides a methodical way to define, organize, and load these assets, maintaining clarity and control in how modules manage and deliver front-end resources. This lesson will guide you through creating a library using the .libraries.yml file.

Understanding the .libraries.yml File

The .libraries.yml file in Drupal acts as a descriptor for assets (CSS, JavaScript) associated with your module or theme. By defining libraries, you specify which assets are grouped and how they should be loaded, helping ensure that your site’s components function and appear consistently using a modular approach.

Setting Up Your Library

Let’s create a library for a custom module, which we'll call my_rest_module. Follow these steps to establish your library file:

Step 1: Create the .libraries.yml File

First, in your module’s directory, create the .libraries.yml file:

      
          touch modules/custom/my_rest_module/my_rest_module.libraries.yml
      
    

Step 2: Define Your Library in YAML

Edit my_rest_module.libraries.yml to define your library’s resources. Below is a simple example that includes CSS and JavaScript assets:

      
          global-styling:
            version: 1.x
            css:
              theme:
                css/style.css: {}
                css/mobile.css: { media: all and (min-width: 768px) }
            js:
              js/script.js: {}
            dependencies:
              - core/jquery
      
    

In this definition:

  • global-styling: The machine name for your library.
  • version: Indicates the version of the library (optional, but useful for maintaining versions).
  • css and js: Specify paths to your CSS and JavaScript files, organized by type (e.g., theme for CSS).
  • dependencies: Declare dependencies required for your library, such as core/jquery.

Step 3: Include the Library Files

Ensure the referenced CSS and JS files exist within your module's directory. Create the respective files within a css and js folder:

      
          mkdir -p modules/custom/my_rest_module/css
          touch modules/custom/my_rest_module/css/style.css
          touch modules/custom/my_rest_module/css/mobile.css

          mkdir -p modules/custom/my_rest_module/js
          touch modules/custom/my_rest_module/js/script.js
      
    

Attaching Your Library

Once your library is defined, attach it to your module’s pages or components. This can be done via hooks in your module file, twig templates, or directly in render arrays.

Example: Attaching via Hook

To attach your library globally to your module's page, use a hook like hook_page_attachments():

      
          /**
           * Implements hook_page_attachments().
           *
           * @param array &$attachments
           *   An associative array of attachments, passed by reference.
           */
          function my_rest_module_page_attachments(array &$attachments) {
            $attachments['#attached']['library'][] = 'my_rest_module/global-styling';
          }
      
    

This hook ensures that your assets load every time the module’s page is rendered.

Testing and Debugging

After configuring your library and attaching it, test the implementation:

  • Ensure your assets are loaded correctly by inspecting your page's source or using browser developer tools.
  • Verify no console errors are related to missing files or incorrect syntax in your .libraries file.

Conclusion

Creating and managing libraries through .libraries.yml is a foundational skill in Drupal that not only optimizes asset management but also enhances the modularity and maintainability of your code. Proper usage leads to better structured and more efficient delivery of your CSS and JavaScript resources.

What’s Next?

Having mastered the creation of libraries, the next logical step in enhancing your module’s functionality is Attaching CSS/JS to Render Arrays or Routes. This allows for dynamic and context-specific asset loading, further improving page performance and user experience.