Adding tags to services for event subscribers or collectorsfor Drupal 8 , 9 , 10 , and 11

Last updated :  

In the previous lessons, we explored how to inject and use services in controllers and plugins. Today, we'll delve into using service tags for event subscribers or service collectors. This lesson will enhance your ability to create responsive and interaction-rich modules in Drupal.

Understanding Service Tags

In Drupal, services are primarily configured using YAML files. Tags in these files are descriptors that associate specific functionalities or responsibilities, such as subscribing to events or acting as collectors. They enable Drupal to identify and handle services in a structured manner.

Why Use Service Tags?

Service tags are especially useful for:

  • Event Subscribers: They allow services to listen for specific events and react to them, fostering a reactive and event-driven module architecture.
  • Service Collectors: They enable the gathering of multiple services of a similar purpose, used collectively to perform complex operations.

Example Scenario: Custom Event Subscriber

We'll create an event subscriber that listens to Drupal's kernel request, altering the response headers to add a custom message. This example will demonstrate the practicality of service tags in event-driven programming.

Step 1: Create the Event Subscriber

Create a new directory named EventSubscriber within your module and add a file named CustomEventSubscriber.php.


// weather_module/src/EventSubscriber/CustomEventSubscriber.php

namespace Drupal\weather_module\EventSubscriber;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

/**
 * Event Subscriber for adding custom headers.
 */
class CustomEventSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = ['onRespond', 100];
    return $events;
  }

  public function onRespond(ResponseEvent $event) {
    $response = $event->getResponse();
    $response->headers->set('X-Custom-Header', 'WeatherModuleHeader');
  }
}

Step 2: Add the Service Definition and Tag

Define your service and tag it as an event subscriber in your module's services.yml file. This enables Drupal to recognize and call your subscriber for specified events.


// weather_module.services.yml

services:
  weather_module.custom_event_subscriber:
    class: 'Drupal\weather_module\EventSubscriber\CustomEventSubscriber'
    tags:
      - { name: 'event_subscriber' }

Step 3: Enable Your Module and Test

Ensure your module is enabled and working correctly. Reload any page on your site, and use browser developer tools or an HTTP client to verify the presence of the X-Custom-Header in the response headers.

Using Service Collectors

Service collectors are another powerful mechanism, allowing services to gather and manage groups of similar tagged services. If your module grows to require multiple similar services (e.g., different authentication mechanisms), collectors offer a unified access point.

Conclusion and Next Steps

Through this lesson, you learned how to add tags to services, creating an event subscriber as an example. This capability is crucial for crafting interactive and robust Drupal modules that respond to system-wide events or conditions.

Next up, we'll explore "Creating custom services", where you will learn to define and register your own services, further expanding the dynamic capabilities of your module.

Keep experimenting, and see you in the next engaging lesson!