Drupal is a registered trademark of Dries Buytaert
cms 2.1.3 Update released for Drupal core (2.1.3)! drupal 10.5.11 Update released for Drupal core (10.5.11)! drupal 11.3.11 Update released for Drupal core (11.3.11)! drupal 11.2.13 Update released for Drupal core (11.2.13)! drupal 10.6.10 Update released for Drupal core (10.6.10)! cms 2.1.2 Update released for Drupal core (2.1.2)! drupal 11.1.10 Update released for Drupal core (11.1.10)! drupal 10.5.10 Update released for Drupal core (10.5.10)! drupal 10.4.10 Update released for Drupal core (10.4.10)! drupal 11.2.12 Update released for Drupal core (11.2.12)! drupal 11.3.10 Update released for Drupal core (11.3.10)! drupal 10.6.9 Update released for Drupal core (10.6.9)! drupal 10.6.8 Update released for Drupal core (10.6.8)! drupal 11.3.9 Update released for Drupal core (11.3.9)! drupal 11.3.8 Update released for Drupal core (11.3.8)! drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)!

ael

Security covered
View on drupal.org

The "AsEventListener" module enables developers to use the Symfony attribute #[AsEventListener] to register event subscribers/listeners.

To register an event subscriber in Drupal you need to create a service and tag
it as event_subscriber. See Subscribe to and dispatch events on drupal.org.

Symfony however defines the attribute #[AsEventListener] so developers are able to register classes or single methods as event listeners. This module brings this feature to Drupal.
It is planned to add this feature to Drupal core. Until #3376163 is fixed, this module provides a way to make use of it now.

Usage

To make use of this module you need to setup classes or methods as event
listeners using #[AsEventListener]. The classes needs to be in namespace Drupal\[...]\EventListener to be registered automatically.

Example

<?php

namespace Drupal\my_custom_module\EventListener;

#[AsEventListener(event: RoutingEvents::ALTER)]
class TestEventListener {

  /**
   * Use the class directly as event listener.
   *
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The dispatched event.
   */
  public function __invoke(RouteBuildEvent $event): void {
    // Do something.
  }

}

This will add this class as event listener for RoutingEvents::ALTER so every time this event is dispatched, TestEventListener::__invoke() is called.

See Defining Event Listeners with PHP Attributes for details about how to use #[AsEventListener].

Activity

Total releases
3
First release
Apr 2026
Latest release
1 month ago
Release cadence
3 days
Stability
100% stable

Release Timeline

Releases

Version Type Release date
1.0.2 Stable Apr 20, 2026
1.0.1 Stable Apr 20, 2026
1.0.0 Stable Apr 15, 2026