0

I wrote a sample custom module (juniper) and created the corresponding juniper.libraries.yml file to link the CSS and JS. I am trying to load this library only on this module pages (/juniper). However, I have issues:

  1. I tried to add the library via juniper.info.yml:
libraries:
  - juniper/june

but this doesn't work for some reason

  1. I added the juniper_page_attachments hook to the juniper.module
function juniper_page_attachments(array &$page) {
  //$page['#attached']['library'][] = 'juniper/june';
}

but it added the library to the whole site, not just the module.

Could you please help me figure out how to limit the library loading only to the custom module page I created? Thanks!

1
  • What does this mean: "but it added the library to the whole site, not just the module."?
    – leymannx
    Mar 15, 2023 at 8:21

2 Answers 2

1

There's a few different ways of doing this, depending on how you're building /juniper.

  • If you have a controller that is returning a render array, you can include something like $build['#attached']['library'][] = 'juniper/june'; to just include it for that page.

  • If you're going to use this library on any /juniper/* page, then something like a hook_page_attachments could be used with logic similar to this:

/**
 * Implements hook_page_attachments().
 */
function juniper_page_attachments_alter(array &$attachments)
{
  $route_uri = \Drupal::request()->getRequestUri();

  if(str_starts_with($route_uri, '/juniper/')) {
    $attachments['#attached']['library'][] = 'juniper/june';
  }
}

Another solution could be to check out \Drupal::routeMatch() if you want to attach to particular routes, or \Drupal::currentUser() if you only want to attach for particular users.

2
  • Yes, the first option is the preferred way and has the desired result that the library is attached when the module is outputting HTML for the library. See drupal.stackexchange.com/questions/237336/…. The second option should be used only if the first is not possible. It works for the current route match, but not for the current user. Pages are not cached by user.
    – 4uk4
    Mar 15, 2023 at 7:47
  • Thank you! The second option worked for me, I just had to slightly modify the route: str_starts_with($route_uri, '/juniper') to match just '/juniper'
    – LBRTYCAT
    Mar 15, 2023 at 18:26
1

You can't attach libraries from module info files. That only works for themes. And yes, if you attach in the page attachments hook without any other condition on the URL or whatever it will be added on every site.

Preferably you only attach to the element where you need either from inside a template via {{ attach_library('your_module/library_name') }} or from a render array maybe from a preprocess hook like hook_preprocess_paragraph etc.

But this is already documented really well in the official docs: Adding assets (CSS, JS) to a Drupal module via *.libraries.yml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.