3

I would like to replace an event subscriber.

Can I write a class that implements EventSubscriberInterface which replaces another event subscriber?

1 Answer 1

6

Since an event subscriber is a service, you can achieve it by creating a class that implements ServiceModifierInterface.

Drupal doesn't have a class that alters an event subscriber (which isn't a class used in tests). Changing the class associated to an even subscriber isn't different from changing the class associated with a generic service, which is what the following code used from the CoreServiceProvider class does, in its alter() method.

$uuid_service = $container->getDefinition('uuid');

// Debian/Ubuntu uses the (broken) OSSP extension as their UUID
// implementation. The OSSP implementation is not compatible with the
// PECL functions.
if (function_exists('uuid_create') && !function_exists('uuid_make')) {
  $uuid_service->setClass('Drupal\\Component\\Uuid\\Pecl');
}
elseif (function_exists('com_create_guid')) {
  $uuid_service->setClass('Drupal\\Component\\Uuid\\Com');
}

$container->getDefinition() returns a Definition instance.

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.