1

According to https://sqndr.github.io/d8-theming-guide/libraries/libraries-and-twig.html, you can include a library within twig template to only load it on specific pages using {{ attach_library('theme/library') }}. This appears to be for libraries included in mytheme.libraries.yml.

Is there a way to use this to include a library from sites/all/libraries? It seems silly to add a library to my theme folder when it is already available globally.

7
  • You can add globally available libraries from modules and drupal core in the same way. - About the second part of your question, can you give more details what libraries you have in /sites/all/libraries, this looks like the folder name the Libraries API module used in D7, which is not ported to D8 yet.
    – 4uk4
    Commented May 5, 2017 at 21:15
  • When the Libraries API module is ready for D8, loading libraries in twig will be supported, see drupal.org/node/1704734#comment-11908472
    – 4uk4
    Commented May 5, 2017 at 21:19
  • @4k4, I didn't realize libraries wasn't ready for D8 yet, actually. Modules are still instructing to add reqs to that folder -- probably just for future-proofing. Specifically, for this I am using drupal.org/project/magnific_popup, so I'm just trying to leverage that js without doubling it up into my theme folder.
    – Mrweiner
    Commented May 5, 2017 at 21:42
  • Yes, some D8 modules instruct you to download them to /libraries by hand. But these libraries can be used only by the module that instructed you to do so, because no one else knows the location of the files.
    – 4uk4
    Commented May 5, 2017 at 21:57
  • So in this case, since I know the location of the files, could I load them from my theme? Or do I need to wait for Libraries API?
    – Mrweiner
    Commented May 5, 2017 at 21:58

1 Answer 1

1

There was likely a module that required the files in question to be added to the sites/all/libraries folder. You could review that module on how it adds those files as libraries. Check that module for a .libraries.yml file or check to see if the module implements hook_library_info_build(). Once you know the how to reference the library, you should be able to use the same reference in your twig template.

As an example, the Easychart module (https://www.drupal.org/project/easychart) has both a .libraries.yml and implements the hook in it's .module file. To reference one of those libraries you would use "easychart/easychart.render" or "easychart/lib.highcharts"

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.