Drupal 8 is a Great Way of Developing a Useful and Valuable Website – Part Eleven


Sharing is caring!

The website development world is truly an amazing world, developers should always keep their skills up to date by taking courses and getting certifications, and training which can help them to accomplish great things, in the web technology space.

In today’s article, we are going to talk a bit more about services in Drupal 8, we will learn how to use one, step by step.

Accessing and using services requires a developer to use, something called Dependency Injection. Setter methods are used to inject services, or they can also be passed, as an argument to a constructor, this way there is no need to call out to the global service container.

Alright, so let’s delve into the structure of a service file, the your_module_name.services.yml file will define the services you would like to use. This file is automatically used and detected by Drupal when it is placed in the root of your_module directory.

It looks something like this, you can find the complete list of services on the core.services.yml file.



gc_probability: 1

gc_divisor: 100


debug: false


required_cache_contexts: [‘languages:language_interface’, ‘theme’, ‘user.permissions’]


max-age: 0


default: keyvalue.database

http.response.debug_cacheability_headers: false


default: keyvalue.expirable.database


– http

– https

– ftp

– news

– nntp

– tel


enabled: false

supportsCredentials: false

tempstore.expire: 604800


# Simple cache contexts, directly derived from the request context.


class: Drupal\Core\Cache\Context\IpCacheContext

arguments: [‘@request_stack’]


– { name: cache.context }


class: Drupal\Core\Cache\Context\HeadersCacheContext

arguments: [‘@request_stack’]


– { name: cache.context }


class: Drupal\Core\Cache\Context\CookiesCacheContext

arguments: [‘@request_stack’]


– { name: cache.context }


class: Drupal\Core\Cache\Context\SessionCacheContext

arguments: [‘@request_stack’]


– { name: cache.context}


class: Drupal\Core\Cache\Context\SessionExistsCacheContext

arguments: [‘@session_configuration’, ‘@request_stack’]


– { name: cache.context}


alias: config.storage.staging



Remember that this is a .yml file, therefore, it is very important that you pay attention to indentation here. Before we move on let’s talk about the properties of a service, to understand the services .yml file a bit better.

The first property is alias, which is just the alias name of the service which you intend to use for your custom code. Aliasing services would give you the ability to access them, through a shortcut created through an alias.

The next property of a service file is the calls, which are used for setter injection; it is important to note that there are three different types of injections known as the constructor injection, which is used by adding an argument to the constructor signature, which can accept the dependency. The constructor injection is the most common way of injecting dependencies in Drupal 8.

The constructor injection allows developers to ensure that required dependencies are present when a class that cannot be constructed without the dependency is used. The constructor injection though can be problematic when a class needs to be overridden or extended.

The setter injection is another way of injecting dependencies into a class, by setting a setter method which accepts the dependency, then we are using the setter injection. Optional dependencies are better injected with setter injection because the setter does not have to be called if the dependency is not needed. With setter injections, you don’t have to call them but just once, you can make multiple calls, to the same setter injection.

The setter injection though can also force you to work in the dark, because a developer doesn’t have a way to know if the dependency has been changed during the lifetime of the object. The only way of knowing this information is to explicitly write the setter method, which would check if the dependency has already been called. In order to find out if any required dependency has been injected a developer has to also add checks when he/she is using the setter injection method.

The other type of dependency injection is known as the property injection, which is like setter injection; however, with property injection, there is no way to control when a dependency has been set, because, during the object’s lifetime, it can be changed. Before using a dependency, which has been injected via the property injection method, a developer has to write code, which would explicitly test the class instance; because developers will not be sure which dependency is being used.

Alright, we have arrived at the end of our conversation, we will continue talking about this topic on the very next article stay tuned.

Thank you for reading this article!!!