Module Development
Topic

Services for Drupal 8, 9, and 10

As a development framework, Drupal core comes with a lot of useful objects for doing things like accessing the database, sending email, or making HTTP requests. Generally speaking almost everything that Drupal does is actually done by one of these objects. In Drupal, these objects are called services and in order to make working with them easier they are generally accessed via the service container.

To keep things flexible and not tied to any one specific way of doing things these service objects implement a known interface and can be exchanged for other services that implement the same interface without affecting the code using the service. This allows Drupal to do things like work with multiple different database servers, or for a module to override Drupal’s mail handling and send all emails through a third party service instead.

As a developer you’ll use the service container to access existing services in order to use the functionality they provide. You’ll define new services in order to encapsulate your unique application logic in a reusable way. Some Drupal subsystems require you to define services and register a service class in order to use them.

Example tasks

  • Request a service from the service container so you can use it in your code. For example, send an email.
  • Define a new service
  • Subscribe to an event
  • Override an existing service

Confidence

Drupal builds on the Symfony service container, which has been quite stable. The way that you define, and use, services has changed very little since they were introduced in Drupal. We don’t anticipate any major changes. This means that older documentation resources are likely still useful long after their publication date.

Drupalize.Me resources

More information

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
More information

In this tutorial, we'll shift gears from routes and controllers to services. First off, you'll learn all about the Service Container, also known as the Dependency Injection Container. This is the basis for understanding these special objects called services and how you can leverage them in Drupal.

Additional resources

Dependency Injection and the Art of Services and Containers

More information

In this tutorial, you'll learn how to refactor the controller and create a service.

Additional resources

Dependency Injection and the Art of Services and Containers — Drupalize.Me

More information

Now that we've created a service, let's configure it. In this tutorial, you'll learn how to register your new service. We'll also be using Drupal Console to ensure everything is working as it should.

Note: The Drupal Console command container:debug is now debug:container.

Note: Drupal Console is no longer maintained. There are other tools and methods for getting a list of services in the container. See the tutorial Discover and Use Existing Services to learn more.

Additional resources

Discover and Use Existing Services
Dependency Injection and the Art of Services and Containers — Drupalize.Me
An Introduction to YAML — Drupalize.Me

More information

In this tutorial, you will learn how to extend the ControllerBase class in Drupal and get services out of the container.

Additional resources

Injecting services in your D8 plugins (lullabot.com)
abstract class ControllerBase — api.drupal.org
PHP Service Container — Drupalize.Me

More information

In this tutorial, we'll explore some of the helper functions in ControllerBase. We'll discover the magic behind these shortcut functions is services!

Additional resources

abstract class ControllerBase — api.drupal.org
PHP Service Container — Drupalize.Me

Categories
Drupal 8, 9, and 10
More information

Dependency injection is a design pattern commonly used in object-oriented software architectures in order to support Inversion of Control.

More information

In this tutorial, you'll learn how to get a service that's in another service by using the special constructor method.

Additional resources

PHP Class Constructors — Drupalize.Me

More information

In this tutorial, we'll inject some configuration into our constructor class and introduce parameters by way of our services YAML file.

Additional resources

An Introduction to YAML — Drupalize.Me

More information

In this tutorial, you'll see how Drupal's development.services.yml file can be used to configure services on a local environment. We'll use it to turn the cache off in our service during local development.

Additional resources

An Introduction to YAML — Drupalize.Me

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate guides

External resources