Score:1

Is "event_subscriber" a reserved naming convention for services? Are there guidelines to follow?

cn flag

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?

Jaypan avatar
de flag
What does your service declaration look like?
cn flag
@Jaypan updated question with before/after code snippets.
4uk4 avatar
cn flag
`my_module.event_subscriber` might be reserved because it is already in use.
Score:2
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.