
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