Change record status: 
Project: 
Introduced in branch: 
10.1.x
Introduced in version: 
10.1.0
Description: 

Core services now provide aliases for Symfony autowiring. This means that if you write a new service that injects core services, you can simplify your service declarations. For example, with a constructor as follows:

  public function __construct(ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, ControllerResolverInterface $controller_resolver) {

And a service in MODULENAME.services.yml:

  user.permissions:
    class: Drupal\user\PermissionHandler
    arguments: ['@module_handler', '@string_translation', '@controller_resolver']

As these core services can now be autowired, your MODULENAME.services.yml can just specify:

  user.permissions:
    class: Drupal\user\PermissionHandler
    autowire: true

or if you want to autowire all services in your MODULENAME.services.yml file:

services:
  _defaults:
    autowire: true
  user.permissions:
    class: Drupal\user\PermissionHandler

The correct service implementations will be injected for you, based upon the interfaces (or classes) declared in the arguments of the service constructor.


Behind the scenes this is achieved by the following additions to core.services.yml:

  Drupal\Core\Extension\ModuleHandlerInterface: '@module_handler'
  Drupal\Core\StringTranslation\TranslationInterface: '@string_translation'
  Drupal\Core\Controller\ControllerResolverInterface: '@controller_resolver'

All standalone core classes and non-ambiguous core interfaces (i.e. interfaces that only have a single implementation) can be autowired.

Impacts: 
Module developers