Skip to main content
All posts
Drupal 11 PHP Architecture Symfony Nov 16, 2026 · 4 min read

Drupal 11 Event Subscribers vs. Hooks: When to Use Which (and Why I'm Migrating Old Hooks)

Drupal's object-oriented event system has been available for years, but hooks still dominate most custom modules. Here's how I decide which pattern to reach for — and the specific cases where I'm actively replacing hooks with event subscribers.

Article

The Coexistence Problem

Drupal 11 ships with both systems fully intact: procedural hooks defined in .module files, and Symfony-style event subscribers wired through the service container. If you're inheriting a codebase written across the last five years, you'll almost certainly have both — sometimes doing the same conceptual job in different places. That's the situation I found myself in recently while auditing a mid-sized site with about 30 custom modules.

The question isn't "which is better" in the abstract. It's about knowing exactly what each system is optimized for and making deliberate choices rather than defaulting to whatever the nearest Stack Overflow answer used.

What Hooks Are Still Good For

Hooks remain the right tool when Drupal's alter system is specifically what you need. If you're modifying a render array, tweaking form elements, or adjusting an entity's field definitions before they're used, hooks are the intended extension point — and there's no event equivalent for most hook_*_alter() invocations.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_node_article_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
    $form['field_summary']['#description'] = t('Keep this under 160 characters for SEO.');
}

Trying to replicate this with an event subscriber would require hooking into FormEvents::PRE_SET_DATA or similar and then filtering by form ID manually — more code, less clarity, no real benefit.

Hooks also win for simplicity in genuinely small, one-off customisations. A five-line hook_mail_alter() that rewrites a subject line doesn't need to be a service with a tagged interface and a YAML entry.

Where Event Subscribers Pull Ahead

The calculus flips the moment you need any of these things:

  • Injected dependencies. Services — loggers, config factories, entity type managers — can't be cleanly injected into a procedural hook. You end up calling \Drupal::service(), which is fine in a pinch but makes testing a chore.
  • Priority control. Multiple modules reacting to the same hook fire in module weight order, which is a global setting and a blunt instrument. Event subscribers declare their own priority per listener, so you can order reactions without touching module weights.
  • Stoppable events. If you need one listener to prevent subsequent listeners from running, Symfony's StopPropagation mechanism is explicit and clean. The hook system has no equivalent.
  • Reacting to Symfony/Drupal kernel events. KernelEvents::REQUEST, KernelEvents::RESPONSE, KernelEvents::EXCEPTION — these are only accessible as event subscribers. There's no hook for them.
// src/EventSubscriber/MaintenanceRedirectSubscriber.php
namespace Drupal\mymodule\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;

class MaintenanceRedirectSubscriber implements EventSubscriberInterface {

  public function __construct(
    private readonly ConfigFactoryInterface $configFactory
  ) {}

  public static function getSubscribedEvents(): array {
    return [
      KernelEvents::REQUEST => ['checkMaintenance', 30],
    ];
  }

  public function checkMaintenance(RequestEvent $event): void {
    $maintenance = $this->configFactory->get('system.maintenance')->get('enabled');
    if ($maintenance && !$event->getRequest()->attributes->get('_maintenance_allowed')) {
      $event->setResponse(new RedirectResponse('/maintenance'));
    }
  }
}

The equivalent in a hook would require hook_init() (which doesn't even exist in Drupal 11) or some awkward shim. The subscriber version is testable, explicit about its priority, and reads like standard Symfony code that any backend developer can follow.

The Migration Pattern I'm Using

When I find a hook that uses \Drupal::service() calls, I treat it as a candidate for conversion. The process is straightforward:

  • Create a new class in src/EventSubscriber/ implementing EventSubscriberInterface.
  • Move the logic, replacing static service calls with constructor-injected dependencies.
  • Register the service in mymodule.services.yml with the event_subscriber tag.
  • Delete the hook implementation.

I don't do this wholesale — only when a hook is already painful to test or when it's handling something that's genuinely event-shaped (a reaction to a state change, not a data mutation). The goal isn't ideological purity; it's reducing friction the next time I need to debug or extend that code.

One Thing to Watch in Drupal 11

Drupal core is progressively dispatching more of its own events. The EntityEvent family, layout builder events, and media source events all landed in recent minor releases. Before writing a hook to intercept entity operations, it's worth checking core/core.services.yml and the relevant module's *.services.yml to see if a dispatched event already exists. Using it means your code is decoupled from the hook invocation order and plays nicely with any other modules that subscribe to the same event.

The practical upshot: hooks aren't going away, and they shouldn't. But every new piece of logic I write that needs a service goes into a subscriber by default now. The hooks I keep are the ones that are genuinely alter-shaped.