-1

So, my theme.libraries.yml file shows:

global-styling:
  css:
    theme:
      css/style.css: {}
      css/custom.css: {}
  js:
    js/myjavascript.js: {}

In mytheme.theme, hook_preprocess_node() I have added:

$variables['#attached']['library'][] = 'myjavascript.js';

Drupal is throwing this error message:

User warning: The following theme is missing from the file system: myjavascript.js in drupal_get_filename() (line 250 of core/includes/bootstrap.inc).

I cannot see where I'm going wrong. The javascript is definitely being injected, since it all works. Why am I getting this error?

thanks

1
  • You can follow topic. With Drupal 8, you need to add library not a file.
    – MrD
    Feb 22, 2018 at 2:30

1 Answer 1

0

To sum it up: You may just omit your manual inclusion/attachment of the JavaScript file in hook_preprocess_node().

Long version:

As @MrD already stated, in Drupal 8 you attach libraries, not files. So with your statement, your Drupal is looking for a library definition myjavascript.js. Which would mean a libraries file like:

myjavascript.js:
  js:
    js/myjavascript.js: {}

As you already added the JavaScript file to your global-styling library, and this library seems to being attached within your theme already, the JavaScript automagically will be included when you use the theme.

For creating an own library that can be attached conditionally, please refer to the answer referenced by MrD.

Not the answer you're looking for? Browse other questions tagged or ask your own question.