0

I was following the documentation about creating a custom hello world module. I got to the point where you are told how to attach custom JavaScript and CSS files using hooks. I want to get a JavaScript file attached on every page. Unfortunately, I can't manage it to work. Everything was working, so far.

This is the code I am using.

hello_world.libraries.yml

hello_world:
  version: 1.x
  css:
    theme:
      css/style.css: {}
  js:
    js/hello_world.js: {}
  dependencies:
    - core/jquery

hello_world.module

function hello_world_page_attachments(array &$attachments) {
  $attachments['#attached']['library'][] = 'hello_world_for_drupal_8/hello_world';
}

hello_world.js

alert("outside script");
console.log("outside script");

1 Answer 1

2

Libraries are refererred to as module_machine_name/library_name

If the other names in your post are accurate, in your case that should be:

$attachments['#attached']['library'][] = 'hello_world/hello_world';
0

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.