About drupal-events
A reference skill for Drupal developers that shows how to define and register event subscribers, handle common kernel events, and generate a subscriber with Drush.
Key features
- 01Define event subscribers
- 02Register in services.yml
- 03Review common kernel events
- 04Generate subscribers with Drush
Use cases
- 01React to request or response events
- 02Handle uncaught exceptions
- 03Generate module subscriber code
Install
Copy the command and run it in a terminal.
$
npx skills add https:/ / github.com/ edutrul/ drupal-ai --skill drupal-eventsAfter installing, type/drupal-eventsin Claude Code to confirm it loaded.
Bundled filesREADME.md
Details
From the project READMEDrupal Event Subscribers
Example (Best Practice)
// src/EventSubscriber/MyEventSubscriber.php
namespace Drupal\my_module\EventSubscriber;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final class MyEventSubscriber implements EventSubscriberInterface {
public function __construct(
private readonly AccountProxyInterface $currentUser,
) {}
public static function getSubscribedEvents(): array {
return [
// Note: Higher priority runs earlier. Default is 0.
KernelEvents::REQUEST => ['onRequest', 100],
];
}
public function onRequest(RequestEvent $event): void {
if (!$event->isMainRequest()) {
return;
}
if (!$this->currentUser->isAuthenticated()) {
return;
}
// Example: log or modify behavior for authenticated users.
}
}
Register in services.yml
services:
my_module.my_event_subscriber:
class: Drupal\my_module\EventSubscriber\MyEventSubscriber
arguments: ['@current_user']
tags:
- { name: event_subscriber }
Common Events
| Event | Constant | When |
|---|---|---|
| Request | KernelEvents::REQUEST | Every request |
| Response | KernelEvents::RESPONSE | Before response sent |
| Terminate | KernelEvents::TERMINATE | After response sent |
| Exception | KernelEvents::EXCEPTION | On uncaught exception |
Generate with Drush
drush generate event-subscriber --answers='{
"module": "my_module",
"class": "MyEventSubscriber",
"event": "kernel.request"
}'
Comments
Markdown supported