In Drupal 8, a service is any object managed by the services container. Drupal 8 introduces the concept of services to decouple reusable functionality and makes these services pluggable and replaceable by registering them with a service container.
Service is a PHP class with some code that provides a single specific functionality throughout the application. So you can easily access each service and use its functionality wherever you need it. Because of that it is easy to test and configure in your application. This is called service-oriented architecture which is not unique to Symfony or even PHP.
The Services and Dependency Injection Container concepts have been adopted by Drupal from the Symfony framework. Accessing the database, sending email, or translating user interface are examples for the services in Drupal 8.
How to define your own service in Drupal 8 through custom module Step are below.
1- create the .info.yml file [custom_services.info.yml]
name: Custom Services
type: module
description: The module provide the sample code to define your service from your custom module.
core: 8.x
package: Custom
2- create the .services.yml file [custom_services.services.yml]
services:
custom_services.say_hello:
class: Drupal\custom_services\HelloServices
File name is ‘custom_services.services.yml’ where the ‘custom_services’ is our module name.
‘custom_services.say_hello’ is the service name defined by us, , Where we need to follow the pattern of ‘module_name’ concatenate with a ‘unique_name’.
We have the class name for services. ‘class: Drupal\custom_services\HelloServices’ which will be kept under the ‘src’ folder.
Also the dependency can be added the following way
arguments: ['@modulename.services1', '@modulename.services4', '@modulename.services7']
In this case there are no dependencies.
For more details visit https://www.drupal.org/node/2194463
3- Create the class ‘HelloServices.php’ under the ‘src’ folder
<?php
/**
* @file providing the service that say hello world and hello 'given name'.
*
*/
namespace Drupal\custom_services;
class HelloServices {
protected $say_something;
public function __construct() {
$this->say_something = 'Hello World!';
}
public function sayHello($name = ''){
if (empty($name)) {
return $this->say_something;
}
else {
return "Hello " . $name . "!";
}
}
}
This is simple class provide the service.
How to access the our own defined service?
Accessing the service globally.
$service = \Drupal::service('custom_services_example.say_hello');
If you want to test this , you can enable the devel module and go to the path ‘devel/php’ and run the following code
$service = \Drupal::service('custom_services.say_hello');
dsm($service);
dsm($service->sayHello('Waliullah'));
So you will get the following output.
Drupal\custom_services\HelloServices Object
(
[say_something:protected] => Hello World!
[_serviceId] => custom_services.say_hello
)
Hello Waliullah!
For more details please visit the following links
https://www.drupal.org/node/2133171
http://symfony.com/doc/current/book/service_container.html