Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: Facets 3.0.6 Minor update available for module facets (3.0.6). Release: Facets 3.0.5 Minor update available for module facets (3.0.5). Release: Flag 5.1.0 Minor update available for module flag (5.1.0). Release: Random Name 1.2.3 Minor update available for module random_name_chooser (1.2.3). Release: Text with summary 1.0.0 Initial release available for module text_with_summary (1.0.0)! Release: Ágora Transparency Theme 1.0.7 Minor update available for theme agora_theme (1.0.7). Release: Automated Revision Log 1.1.3 Minor update available for module ai_revision_log (1.1.3). Release: Views HTTP Client 1.0.4 Minor update available for module views_http_client (1.0.4). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Module Revived: ЮKassa (YooMoney) для Drupal v7 2.6.1 Module yoomoney_api updated after 7 months of inactivity (2.6.1).

AsEventListener

Security covered Drupal 11 · not 10
View on drupal.org

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].

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

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

Release Timeline

Releases

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