0

I've got an event that is triggered on hook_user_login in a module.

$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch('mymodule_user_login', $event);

I also have a subscriber that I think should be catching this event.

public function mymodule_user_login(GetResponseEvent $event) {...

static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['mymodule_user_login', 1];
    return $events;
}

The event seems to fire when I log in, however the subscriber runs on every page load afterwards.

Do I need to somehow clear the event in the subscriber?

1 Answer 1

1

Your code is subscribing to a kernel event KernelEvents::REQUEST dispatched on every request.

But you want to subscribe to the custom event 'mymodule_user_login'. The array value is the callback method, not the event name

static function getSubscribedEvents() {
    $events['mymodule_user_login'][] = ['onUserLoginCallback', 1];
    return $events;
}

which receives the custom event object, not a core event:

public function onUserLoginCallback(MyEvent $event) {
  ...
}

BTW the dispatching code is deprecated. In Drupal 9 or higher swap both arguments:

$event_dispatcher->dispatch($event, 'mymodule_user_login');

See https://symfony.com/blog/new-in-symfony-4-3-simpler-event-dispatching

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.