3

In Drupal 8, when I declare a library in a .libraries.yml file, it seems the library files have to be located INSIDE the module or the theme folder with the .libraries.yml file.

For instance, if a MYMODULE module contains this .libraries.yml file:

form-alter-css:
  version: 1.x
  css:
    theme:
      css/form-alter.css: {}

The file css/form-alter.css has to live at DRUPAL_ROOT/modules/MYMODULE/css/form-alter.css.

Instead, for shared libraries, I'd like to place the files in DRUPAL_ROOT/libraries, like we did in Drupal 7. But how?

I have tried entering an absolute path in the .libraries.yml file (e.g. /libraries/css/form-alter.css) but it doesn't work. Any ideas?

3
  • You should not share libraries at all in Drupal 8. Instead you should declare library dependency in that second library.
    – user21641
    Commented Dec 18, 2015 at 11:14
  • So how would you go about sharing a library between two custom modules? You need to create a third module to declare the library and place the library files in that third module? Commented Dec 18, 2015 at 11:55
  • No, you just add dependency in the second library to the first one + most likely add also module dependency into module info file.
    – user21641
    Commented Dec 18, 2015 at 12:07

2 Answers 2

2

In drupal 7 the shared libraries were provided by a contrib module. Because a lot of modules had this as a dependency, it was very common:

Libraries API

I don't know, if this will be used this often in drupal 8, you can follow the development here:

https://www.drupal.org/node/1704734

So at the moment you are stuck with the libraries in *.libraries.yml, when you don't want to use the dev version of the Libraries API.

The libraries in *.libraries.yml were not designed for libraries in a shared directory. But they have one very useful feature, you can borrow a library from another module. There are a lot of useful packages already in core modules, like jquery ui elements. If you define a library in one of your modules, they also become available for all other modules. But the files will remain local to that module, which defines the library.

2
  • I do realize that a library in module A can depend on a library in module B, but when the library is not specific to either module it feels a bit arbitrary to include it in either. Commented Dec 18, 2015 at 16:00
  • I agree with you. That was the purpose of Libraries API in D7. Share common libraries, that are not specific to one module. We have to wait, what will happen with this in D8. In the meantime *.libraries.yml is the only option you have.
    – 4uk4
    Commented Dec 18, 2015 at 16:16
0

Actually, it's pretty straightforward in D8. To set it up, create a folder named libraries in your root install folder, and add your external libraries there. Covering this is more detail, is a listing here.

1
  • the issues the OP is asking about is how to register a library that is in the root libraries folder. Commented Apr 21, 2021 at 23:37

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.