0

Here is my code:

services file

  services:
  my_module.event_subscriber:
    class: Drupal\my_module\EventSubscriber\My_moduleSubscriber
    arguments:
      - '@authentication'
      - '@current_user'
    tags:
      - {name: event_subscriber}

My_moduleSubscriber.php

 <?php

  public function xyz) {
  // I want to attach js here
  $attachement['#attachement'][js] = .....
//    drupal_add_js(libraries_get_path('audio-player').'/audio-player.js');

  }

  static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = array('xyz');
    return $events;
  }

Since there is no argument passed like $attachement, $page, in which variable should i attach js file in Drupal?

0

A response event is possibly too late, I think at that point, you already have a built response object, and the page is just a string that will be replaced.

You're better of using a hook for this, namely hook_page_attachments(), which exists just for the purpose of adding additional libraries to the page.

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.