Score:0

Attaching a library to only one custom module

us flag

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!

leymannx avatar
ne flag
What does this mean: _"but it added the library to the whole site, not just the module."_?
Score:1
eh flag

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.

4uk4 avatar
cn flag
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 https://drupal.stackexchange.com/questions/237336/attach-library-to-a-specific-page. 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.
LBRTYCAT avatar
us flag
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'
Score:1
ne flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.