I would like to replace an event subscriber. Is there any way that I can write my class implementing EventSubscriberInterface which would replace another existing event subscriber instead of being performed earlier or later that the one existing.


You can do this by creating a ServiceProvider class.

Example service provider class to override the path_subscriber service.

namespace Drupal\MY_MODULE;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;

// @note: You only need Reference, if you want to change service arguments.
use Symfony\Component\DependencyInjection\Reference;

 * Modifies the path_subscriber service.
class MyModuleServiceProvider extends ServiceProviderBase {

   * {@inheritdoc}
  public function alter(ContainerBuilder $container) {
    // Overrides language_manager class to test domain language negotiation.
    // Adds entity_type.manager service as an additional argument.
    $definition = $container->getDefinition('path_subscriber');
      // Custom arguments.
      ->addArgument(new Reference('some_argument'));

Another solution is to override the service in MY_MODULE.services.yml, like this:

  class: Drupal\MY_MODULE\MyCustomPathSubscriber
  arguments: [# The arguments]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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