Module Development

Discover and Use Existing Services for Drupal 8, 9, and 10

It's best practice to access any of the services provided by Drupal via the service container to ensure the decoupled nature of these systems is respected. In order to do so, you need to know what services exists, and then, where possible, use dependency injection to use them in your code.

This tutorial walks through the process of:

  • Discovering existing services and learn their machine name
  • Using the machine name of service to request a copy from the service container

Drupal Module Development