I have begun trying to port a small, simple module that I maintain to Drupal 8.

It was originally written (not by me) in Drupal 6 and used hook_init() in combination with drupal_add_js() to attach its scripts to every page.

Both hook_init() and drupal_add_js() have been deprecated in Drupal 8. I only really need to attach scripts to pages that have a taxonomy term reference widget (and in fact much of my logic has already been moved into a hook_field_widget_taxonomy_autocomplete_form_alter().) Scripts are now added via #attached which I understand, but since I don't have the $form array or object in hook_field_widget_taxonomy_autocomplete_form_alter() I'm not sure where to add the #attached element.

I'm also not clear if #attached is used instead of or in addition to a *.libraries.yml file. Does every single module with its own script.js need to declare a *.libraries.yml file?

How do I attach my custom js file to every page with a taxonomy term reference widget in Drupal 8?


Ok, so here's what I had to do. In mymodule.libraries.yml:

  version: 1.x
    js/myscript.js: {}
    - core/jquery

In hook_libraries_info(): Nothing. I didn't use this.

In hook_field_widget_taxonomy_autocomplete_form_alter():

  $path = drupal_get_path('module', 'mymodule');
  $element['#attached']['js'][$path . '/js/myscript.js'] = array('every_page' => TRUE);

I'm still figuring out how exactly to get jQuery working in the script itself but this attaches the javascript to the page.

  • 1
    libraries.yml replaced hook_libraries_info(), so that is no longer required. You are not using your defined library either, which should not be named like this. base => drupal.yourmodule.something. Then instead of 'library' and specify the name of your library. See for example api.drupal.org/api/drupal/… (search for #attached there)
    – Berdir
    Aug 24 '14 at 10:49
  • So it should be like base: drupal.mymodule.js: instead of base: js: ?
    – beth
    Aug 24 '14 at 17:00
  • 1
    not sure how you came up with base, you shouldn't have base at all. instead of base, have drupal.yourmodule.something.
    – Berdir
    Aug 25 '14 at 18:56
  • You are still not actually using your defined library as you still reference your js file directly in #attached. You need to switch to something like [#attached][library][] = drupal.mymodule.widget;
    – Berdir
    Aug 26 '14 at 20:46
  • When I do that it does not attach the js to the page. ` $element['#attached']['library'][] = drupal.mymodule.widget; in hook_field_widget_taxonomy_autocomplete_form_alter() and the js file is in mymodule/js/script.js.
    – beth
    Aug 26 '14 at 22:01

'#attached' works for any element. You should have an element in scope in that alter hook. Also you should have a mymodule.libraries.yml to declare your script.

  • I have mymodule.libraries.yml and hook_libraries_info(). Still not attaching to my element.
    – beth
    Aug 24 '14 at 9:08
  • You don't need hook_libraries_info(). You need to attach the library to the $element in your hook_field_widget_taxonomy_autocomplete_form_alter(&$element, FormStateInterface $form_state, $context) Aug 30 '14 at 2:23

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.