You are here

public function TaggedHandlersPass::process in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php \Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass::process()
  2. 9 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 at least one argument, optionally more:

    • The handler instance must be the first method parameter, and it must have a type declaration.
    • If the method has a parameter named $id, in any position, it will receive the value of service ID when called.
    • If the method has a parameter named $priority, in any position, it will receive the value of the tag's 'priority' attribute.
    • Any other method parameters whose names match the name of an attribute of the tag will receive the value of that tag attribute.The order of the method parameters and the order of the service tag attributes do not need to match.

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 105

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 results in
  // additional iterations around all the service definitions.
  foreach ($container
    ->getDefinitions() as $id => $definition) {
    foreach ($definition
      ->getTags() as $name => $info) {
      $this->tagCache[$name][$id] = $info;
    }
  }
  foreach ($this->tagCache['service_collector'] ?? [] as $consumer_id => $tags) {
    foreach ($tags as $pass) {
      $this
        ->processServiceCollectorPass($pass, $consumer_id, $container);
    }
  }
  foreach ($this->tagCache['service_id_collector'] ?? [] as $consumer_id => $tags) {
    foreach ($tags as $pass) {
      $this
        ->processServiceIdCollectorPass($pass, $consumer_id, $container);
    }
  }
}