Drupal is a registered trademark of Dries Buytaert
Release: Drupal 11.4.5 Update released for Drupal core (11.4.5)! Release: Drupal 10.6.15 Update released for Drupal core (10.6.15)! Release: Superfish Dropdown Menu 8.x-1.15 Minor update available for module superfish (8.x-1.15). Release: Leaflet 10.4.10 Minor update available for module leaflet (10.4.10). Release: Raven: Sentry Integration 7.5.1 Minor update available for module raven (7.5.1). Release: htmLawed 8.x-3.8 Minor update available for module htmlawed (8.x-3.8). Release: Raven: Sentry Integration 7.5.0 Minor update available for module raven (7.5.0). Module Revived: Media Library Form API Element 2.1.5 Module media_library_form_element updated after 10 months of inactivity (2.1.5). Release: Acquia Content Optimization 2.1.0-alpha1 First alpha version released for module conductor (2.1.0-alpha1). Usage Milestone: Manage display Module manage_display crossed 1,000 active installs.

This module allows developers to register event listeners in Drupal using Symfony's #[AsEventListener] attribute. This provides a more streamlined way to subscribe to and react to events within your Drupal site, similar to how it's done in Symfony. It offers an alternative to the traditional Drupal service tagging method.

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

Tracked releases
3
Tracked since
Apr 2026
Latest release
3 months ago
Releases (12 mo)
3 ▲ from 0
Maintenance
Active

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