2

I am using the following code.

wk_elastic.info.yml

name: Blablabla
description: Blablabla
package: BlaBlaBla

type: module
core: 8.x

libraries:
  - wk_elastic/list-js

dependencies:
  - block

wk_elastic.libraries.yml

list-js:
  js:
    js/list.js: {}
  dependencies:
    - core/jquery

wk_elastic.module

function wk_elastic_preprocess_page(&$variables) {
  $variables['#attached']['library'][] = 'wk_elastic/list-js';
}

The library is not being loaded.

What can I do to fix this?

  • I'm not sure if this is best practice, but I have not needed to add 'core/jquery' as a dependency in my library, and still been able to add it into my closure via jQuery. Maybe that helps? – mradcliffe May 9 '16 at 14:49
  • 1
    Well, just tips, library include on preprocess_page its not needed, once you already included in your .info.yml file (docs: api.drupal.org/api/drupal/…). Have you tried include your library version? – Vagner May 9 '16 at 17:40
1

Instead of attaching in preprocess_page you already attached the library in your theme file:

wk_elastic.info.yml

libraries:
  - wk_elastic/list-js

You may also want to define the version in your libraries file, make sure that your .js file exists (js/list.js) and that there's no module with the same name as your theme:

wk_elastic.libraries.yml

list-js:
  version: 1.x
  js:
    js/list.js: {}
  dependencies:
    - core/jquery
2

Switch the active theme to a core theme. If the library loads now then make sure that your custom theme has <js-bottom-placeholder token="{{ placeholder_token }}"> and <js-placeholder token="{{ placeholder_token }}"> in the html.html.twig template.

Another pitfall is an incorrect reference to the library. Although it does not apply to the original question I'll state it anyway because it solved the problem for me. If your my_module.libraries.yml looks like this

my_libraries.my-library:
  js: ...

then you have to use

['#attached']['library'][] = ['my_module/my_libraries.my-library']

instead of

['#attached']['library'][] = ['my_libraries.my-library']
  • That is not correct. The core.libraries.yml file defines drupal and drupalSettings, which are referred (externally and in dependencies) as core/drupal and core/drupalSettings. – kiamlaluno Feb 4 '17 at 15:09
  • @kiamlaluno yes, that is the case. My point is that when libraries are defined like drupal.active-link in the core.libraries.yml file one could easily miss the core/ in [#attached']['library'][] = 'core/drupal.active-link'. At least that happened to me. – daniels Feb 4 '17 at 15:33
  • That is not the case of the OP, since he is referring his own library as wk_elastic/list-js, and jQuery as core/jquery. – kiamlaluno Feb 4 '17 at 15:43
1

Try renaming "list-js" to "list_js". I've had problems with using hyphens before in naming.

1

One more possible case causing described issue is if you have a module/profile named the same as a theme or vice-versa.

See: https://www.drupal.org/project/drupal/issues/371375

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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