On Drupal 8 Libraries and Custom Modules

Submitted by Marc on Fri, 04/27/2018 - 09:42

So this took me about an hour to diagnose. Most of the documentation for adding a Drupal 8 library (groupings of CSS and JS) to a custom (thing) is based on a custom theme or child theme. So, they say you do the following in your mytheme.info.yml:

libraries:
  - fluffiness/global-styling
  - fluffiness/global-scripts

That's all fine and good for a theme, but doesn't work for a module. Instead this is how you need to do it in a module. Basically, you attach it dynamically if something appears on the page, via a render array, or via preprocess hooks.

I thought for sure I could use the theme way of adding libraries, but that's not the case! Live and learn!