Advent Calendar day 2 - Autowiring all the things

Door number 23
23
Door number 14
14
Door number 18
18
Door number 20
20
Door number 9
9
Door number 13
13
Door number 3
3
Door number 10
10
Door number 21
21
Door number 5
5
Door number 11
11
Door number 8
8
Door number 16
16
Door number 12
12
Door number 6
6
Door number 19
19
Door number 4
4
Door number 17
17
Door number 22
22
Door number 7
7
Door number 15
15
Door number 24
24
By james, 1 December, 2025
Door 2 containing a UK plug representing autowiring
Luca Lusso in the mountains

Today we are getting a bit technical and diving into Autowiring with Luca Lusso.

What is autowiring? Well, since version 8, Drupal has used Services to provide many small pieces of functionality. Basically, a service is a black box you can use to do something on your site. For example, the Messenger service displays status messages on a page.

In Drupal 8 and 9, when you used a service in your code, you needed to tell Drupal what service you wanted. This was a bit messy, because you needed to find out the class that the service creates, and you also needed the service name. It also required you to have create functions, which have a few potential problems and Luca describes as an “anti-pattern”.

Auto-wiring allows Drupal to find the services your code needs without needing to explicitly name them. Essentially you can just use the types in your constructor, and Drupal will find the right services to satisfy them. In a lot of cases it will allow the create functions to be completely removed, simplifying codebases and reducing potential for errors.

You can watch Luca’s full talk here:

Comments

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.