0

I've read Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module and I can see where you can add assets that are part of your module or from a CDN or external source. What about if that library you want to add is already a core asset?

I'm trying to add jquery.ui.accordion to my modules so in my *.libraries.yml I have:

my_module:
  js:
    /core/assets/vendor/jquery.ui/ui/widgets/accordion-min.js: { minified: true }
  dependencies:
    - core/jquery.ui
    - core/jquery.ui.widget

That library is never loaded. What am I missing? Is there a better way to add core assets to my module?

EDIT: I've also tried different paths like:

core/assets/vendor/jquery.ui/ui/widgets/accordion-min.js: { minified: true }
../../../core/assets/vendor/jquery.ui/ui/widgets/accordion-min.js: { minified: true }
2
  • I’m not sure your approach is right here
    – Kevin
    Feb 14, 2019 at 21:23
  • I'd agree with that. I switched it up to my answer below and that seems to work for me for now. I've been working with Drupal 8 for less than a month and Drupal itself for less than a year.
    – frmt
    Feb 14, 2019 at 21:45

1 Answer 1

0

I just scraped the MY_MODULE.libraries.yml and utilized one of the other methods of adding assets, by:

function MY_MODULE_page_attachments(array &$attachments) {
  $attachments['#attached']['library'][] = "core/jquery.ui.accordion";
  //new addition below
  $attachments['#attached']['library'][] = "MY_MODULE/MY_MODULE.accordion";
}

This works for me. Perhaps you don't need a MY_MODULE.libraries.yml if you are trying to add libraries that already exist in core.

Edit: So I still have a MY_MODULE.libraries.yml file however it now just adds my module's javascript (with a dependency to jQuery). It's just some Javascript that initializes jQuery UI accordions. I still use the code above to add jQuery s UI accordion javascript and my own.

1
  • If the library is already defined by core, you don't redefine it in your module, you just add it to #attached as you did in the line $attachments['#attached']['library'][] = "core/jquery.ui.accordion";
    – Jaypan
    Feb 15, 2019 at 2:49

Your Answer

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

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