1

So I was working on a project today, and an event subscriber we had written the better part of a year ago was determined to no longer be firing. Here is the definition:

services:
  my_module.event_subscriber:
    class: Drupal\my_module\EventSubscriber\XYZFeeds
    tags:
      - { name: event_subscriber }

I went through the normal debugging, and found that the event was not being added to the registry. Further debugging uncovered that having the name contain event_subscriber was causing it to not get picked up. If I changed that to anything else it would work, for example:

services:
  my_module.my_module_xyz_subscriber:
    class: Drupal\my_module\EventSubscriber\XYZFeeds
    tags:
      - { name: event_subscriber }

I cannot find any documentation that explicitly states that you cannot use that phrase in a service name.The only convention I am aware of is to always prefix the service name with the module it comes from.

Did something change from D8 to D9, or does this come from the Symfony Framework components that are part of Drupal?

3
  • What does your service declaration look like?
    – Jaypan
    May 17, 2022 at 18:03
  • @Jaypan updated question with before/after code snippets. May 17, 2022 at 18:08
  • my_module.event_subscriber might be reserved because it is already in use.
    – 4uk4
    May 17, 2022 at 19:29

2 Answers 2

2

It doesn't appear to be reserved/disallowed by Drupal or Symfony - this works fine for me in 9.3.13 after adding the code and rebuilding cache (no extra steps):

custom_module.services.yml

services:
  custom_module.event_subscriber:
    class: Drupal\custom_module\EventSubscriber\XYZFeeds
    tags:
      - { name: event_subscriber }

src/EventSubscriber/XYZFeeds.php

<?php

namespace Drupal\custom_module\EventSubscriber;

use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class XYZFeeds implements EventSubscriberInterface {

  public static function getSubscribedEvents() {
    return [
      ConfigEvents::SAVE => 'configSave',
      ConfigEvents::DELETE => 'configDelete',
    ];
  }


  public function configSave(ConfigCrudEvent $event) {
    $config = $event->getConfig();
    \Drupal::messenger()->addStatus('Saved config: ' . $config->getName());
  }

  public function configDelete(ConfigCrudEvent $event) {
    $config = $event->getConfig();
    \Drupal::messenger()->addStatus('Deleted config: ' . $config->getName());
  }

}

This produces the expected messages on config save/delete, so it seems likely your problem is something more localised.

One idea for further debugging that springs to mind is to check that you have no custom (or even contrib) code which is altering the service, perhaps even removing it. This can be done using a service provider, so grep-ing the relevant folders for ServiceProvider might be a first step.

It would also be worth a test to change the first part of the ID rather than the second, to see if is indeed event_subscriber that's causing the problem, or the string as a whole:

services:
  my_module_test.event_subscriber:
    class: Drupal\my_module\EventSubscriber\XYZFeeds
    tags:
      - { name: event_subscriber }

If the altered version works with .event_subscriber, at least you've ruled that out as the problem.

1

The service name can contain a tag name. Drupal core itself has services like form_test.event_subscriber, migrate.plugin_event_subscriber, or module_install_class_loader_test1.event_subscriber.

The fact their names end in event_subscriber makes more evident those services are event subscribers. That's not a requirement for the service to be recognized as event subscriber, though, as Drupal see those as even subscribers because those services use event_subscriber as one of their tags.

To answer to the questions in the question title, event_subscriber isn't a reserved naming convention for services, and there are any guidelines to follow in choosing an event subscriber name, except that two event subscribers cannot have the same name.
To answer the questions in the question body, nothing changed in Drupal 9, in its code or in the code used by Symfony that could cause the issue described in the question. The services I linked before are implemented in Drupal 9, which means that even in Drupal 9 a service with a name that ends in event_subscriber isn't an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.