0

I want to include a CSS and a JavaScript file, without linking them to a theme in particular.

Sometimes, I create small sites without modifying the theme at all, but I still need some minor CSS/JavaScript changes that don't justify creating a sub-theme. I wouldn't mind creating two "quick" and "global" files directly on the Drupal directory just for that.

Can this be done?

0

1 Answer 1

5

Add a module, give it a libraries file (MODULE.libraries.yml):

global:
  js:
    /js.js: {}
  css:
    theme:
      /css.css: {}

In the .module file, implement hook_page_attachments:

function MODULE_page_attachments(array &$attachments) {
  $attachments['#attached']['library'][] = 'MODULE/global';
}

When you enable the module (or clear cache if it's an existing module), the CSS and JS will be included on every page.

0

Your Answer

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