function TaggedHandlersPass::process

Same name in other branches
  1. 9 core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php \Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass::process()
  2. 10 core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php \Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass::process()
  3. 11.x core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php \Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass::process()

Finds services tagged with 'service_collector' or 'service_id_collector', then finds all corresponding tagged services.

The service collector adds a method call for each to the consuming/collecting service definition.

The service ID collector will collect an array of service IDs and add them as a constructor argument.

Supported tag attributes:

  • tag: The tag name used by handler services to collect. Defaults to the service ID of the consumer.
  • required: Boolean indicating if at least one handler service is required. Defaults to FALSE.

Additional tag attributes supported by 'service_collector' only:

  • call: The method name to call on the consumer service. Defaults to 'addHandler'. The called method receives two arguments:

    • The handler instance as first argument.
    • Optionally the handler's priority as second argument, if the method accepts a second parameter and its name is "priority". In any case, all handlers registered at compile time are sorted already.

Example (YAML):


tags:
  - { name: service_collector, tag: breadcrumb_builder, call: addBuilder }
  - { name: service_id_collector, tag: theme_negotiator }

Supported handler tag attributes:

  • priority: An integer denoting the priority of the handler. Defaults to 0.

Example (YAML):


tags:
  - { name: breadcrumb_builder, priority: 100 }

Throws

\Symfony\Component\DependencyInjection\Exception\LogicException If the method of a consumer service to be called does not type-hint an interface.

\Symfony\Component\DependencyInjection\Exception\LogicException If a tagged handler does not implement the required interface.

\Symfony\Component\DependencyInjection\Exception\LogicException If at least one tagged service is required but none are found.

File

core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php, line 90

Class

TaggedHandlersPass
Collects services to add/inject them into a consumer service.

Namespace

Drupal\Core\DependencyInjection\Compiler

Code

public function process(ContainerBuilder $container) {
    // Avoid using ContainerBuilder::findTaggedServiceIds() as that we result in
    // additional iterations around all the service definitions.
    foreach ($container->getDefinitions() as $consumer_id => $definition) {
        $tags = $definition->getTags();
        if (isset($tags['service_collector'])) {
            foreach ($tags['service_collector'] as $pass) {
                $this->processServiceCollectorPass($pass, $consumer_id, $container);
            }
        }
        if (isset($tags['service_id_collector'])) {
            foreach ($tags['service_id_collector'] as $pass) {
                $this->processServiceIdCollectorPass($pass, $consumer_id, $container);
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.