mcpField
D
drupal-events

drupal-events

github.com/edutrulDevelopment7349 installsUpdated 21w ago
Repository

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

  1. 01Define event subscribers
  2. 02Register in services.yml
  3. 03Review common kernel events
  4. 04Generate subscribers with Drush

Use cases

  1. 01React to request or response events
  2. 02Handle uncaught exceptions
  3. 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-events

After installing, type/drupal-eventsin Claude Code to confirm it loaded.

Bundled filesREADME.md

Details

From the project README

Drupal 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

EventConstantWhen
RequestKernelEvents::REQUESTEvery request
ResponseKernelEvents::RESPONSEBefore response sent
TerminateKernelEvents::TERMINATEAfter response sent
ExceptionKernelEvents::EXCEPTIONOn uncaught exception

Generate with Drush

drush generate event-subscriber --answers='{
  "module": "my_module",
  "class": "MyEventSubscriber",
  "event": "kernel.request"
}'

Comments

Markdown supported