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

feature-top

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.

parameters:

session.storage.options:

gc_probability: 1

gc_divisor: 100

twig.config:

debug: false

renderer.config:

required_cache_contexts: ['languages:language_interface', 'theme', 'user.permissions']

auto_placeholder_conditions:

max-age: 0

factory.keyvalue:

default: keyvalue.database

http.response.debug_cacheability_headers: false

factory.keyvalue.expirable:

default: keyvalue.expirable.database

filter_protocols:

- http

- https

- ftp

- news

- nntp

- tel

cors.config:

enabled: false

supportsCredentials: false

tempstore.expire: 604800

services:

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

cache_context.ip:

class: Drupal\Core\Cache\Context\IpCacheContext

arguments: ['@request_stack']

tags:

- { name: cache.context }

cache_context.headers:

class: Drupal\Core\Cache\Context\HeadersCacheContext

arguments: ['@request_stack']

tags:

- { name: cache.context }

cache_context.cookies:

class: Drupal\Core\Cache\Context\CookiesCacheContext

arguments: ['@request_stack']

tags:

- { name: cache.context }

cache_context.session:

class: Drupal\Core\Cache\Context\SessionCacheContext

arguments: ['@request_stack']

tags:

- { name: cache.context}

cache_context.session.exists:

class: Drupal\Core\Cache\Context\SessionExistsCacheContext

arguments: ['@session_configuration', '@request_stack']

tags:

- { name: cache.context}

config.storage.sync:

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!!!

feature-top
feature-top

Add a Comment

Hernando Cadet

Hi every one, I obtained a bachelor's degree in Bioinformatics back in 2006, from Claflin University, after I received my bachelor's degree, I gained full time employment as a software engineer at a Video Relay Service company, maintaining databases and developing software for a new developed device called the VPAD.

I worked at that company for two years, then I became a web developer, and worked for a magazine for three years. After that job, I worked as a Drupal web developer, as a subcontractor for the NIH, for a year and then left the job to go back to school.

Hernando Cadet

Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without

Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without revolutionary ROI.