- 8.2.x core/core.api.php service_tag
- 8.0.x core/core.api.php service_tag
- 8.1.x core/core.api.php service_tag
- 8.3.x core/core.api.php service_tag
Service tags overview
Some services have tags, which are defined in the service definition. Tags are used to define a group of related services, or to specify some aspect of how the service behaves. Typically, if you tag a service, your service class must also implement a corresponding interface. Some common examples:
- access_check: Indicates a route access checking service; see the Menu and routing system topic for more information.
- cache.bin: Indicates a cache bin service; see the Cache topic for more information.
- event_subscriber: Indicates an event subscriber service. Event subscribers can be used for dynamic routing and route altering; see the Menu and routing system topic for more information. They can also be used for other purposes; see http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscri... for more information.
- needs_destruction: Indicates that a destruct() method needs to be called at the end of a request to finalize operations, if this service was instantiated. Services should implement \Drupal\Core\DestructableInterface in this case.
- context_provider: Indicates a block context provider, used for example by block conditions. It has to implement \Drupal\Core\Plugin\Context\ContextProviderInterface.
- http_client_middleware: Indicates that the service provides a guzzle middleware, see https://guzzle.readthedocs.org/en/latest/handlers-and-middleware.html for more information.
Creating a tag for a service does not do anything on its own, but tags can be discovered or queried in a compiler pass when the container is built, and a corresponding action can be taken. See \Drupal\Core\Render\MainContent\MainContentRenderersPass for an example of finding tagged services.
See Services and Dependency Injection Container for information on services and the dependency injection container.
core.api.php, line 2461
- Documentation landing page and topics, plus core library hooks.