Aliases for autowiring in the Drupal services

Julian Andres Cañaveral Valencia
3 min readFeb 22, 2023

--

This article will be divided into 2 parts, Autowire and Aliases for the autowiring.

Autowire:

When using dependency injection (design pattern) on Drupal, it is so common to forget the service name that we want to use and due to that, the site will show an issue that suggests adding the call to the service name, so to fix it, we need search it using Drush command for listing all services names and look up and find the service name that we need to add or look up for it in some own code existing (Stackoverflow does not fail XD).

There is a way to avoid the before mentioned by adding the service automatically. We are going to use the “Autowire” feature that we have had since the Drupal 8.2 version, this feature comes from Symfony 2.8 where the Autowire feature was integrated.

Let’s do it in the code:

Current code:

We have a custom module called “autowire_example” and inside it, in the path src/Service we have a service called AutowireExampleService.php with its interface respective, in my case I called it AutowireExampleInterface.php

In the service file, we have the next code:

<?php

namespace Drupal\autowire_example\Service;

use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
* AutowireExampleService service.
*/
class AutowireExampleService implements AutowireExampleInterface {

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Constructs an AutowireExampleService object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}

/**
* Method description.
*/
public function doSomething() {
// @DCG place your code here.
}

}

As we can see, in the “__contruct” function we are invoking the “Entity type manage” service and to avoid the problem said at the beginning of this article, this service has to be named in the arguments inside the autowire_example.services.yml file:

services:
autowire_example.example:
class: Drupal\autowire_example\Service\AutowireExampleService
arguments: ['@entity_type.manager']

Autowire attribute in the services.yml

To use the Autowire attribute, we can delete the “arguments” line and add the Autowire attribute.

services:
autowire_example.example:
autowire: true
class: Drupal\autowire_example\Service\AutowireExampleService

With “autowire:true” attribute, Symfony will look up all interfaces of
the services invoked in the “__construct” function.

Now, if we have multiple services we can set Autowire to true by default for all of them.

services:
_defaults:
autowire: true
autowire_example.example:
class: Drupal\autowire_example\Service\AutowireExampleService
autowire_example.service2:
class: Drupal\autowire_example\Service\AutowireExampleTwoService

Create Aliases for autowiring

The Autowire feature works using the aliases of each service, but if at any time we need some service that does not has an alias, the Autowire will not work, so for that, we need to create its own alias using the interface.

We are going to suppose the “Entity type manager” does not have an alias and we want to inject it in our service “AutowireExampleService”, in that case, we need to add the EntityTypeManagerInterface class and an alias name, with we can inject it and call from “__contruct” function of the service.

services:
_defaults:
autowire: true
Drupal\Core\Entity\EntityTypeManagerInterface: '@entity_type.manager'
autowire_example.example:
class: Drupal\autowire_example\Service\AutowireExampleService

This is the alias code line.

Drupal\Core\Entity\EntityTypeManagerInterface: '@entity_type.manager'

Change record link: https://www.drupal.org/node/3323122

--

--