What are the Drupal 10 services, and how do you call them in Drupal 10, and what are the core services?

By toswebdev, 7 February, 2023
What are the Drupal 10 services, and how do you call them in Drupal 10, and what are the core services?

Drupal 10 is the latest version of the Drupal content management system and brings a lot of new features and improvements over Drupal 8. One of the key aspects of Drupal 9 is its use of services, which are reusable objects that perform specific tasks within your site. In this blog post, we will discuss what services are in Drupal 9 and provide some examples of how you can use them in your own projects.

What are Services in Drupal 10 ?

A service in Drupal 9 is a reusable piece of functionality that is designed to perform a specific task. These tasks can range from simple actions like sending an email to more complex processes like handling user authentication. Services are defined in the core of Drupal and can be used by other parts of the system, such as modules, to perform specific tasks.

Some common examples of services in Drupal 9 include:

  • mail.manager: This service is responsible for sending emails from your Drupal site.
  • path.alias_storage: This service is used for managing URL aliases in your Drupal site.
  • authentication: This service is responsible for handling user authentication in your Drupal site.

Using Services in Drupal 10

In Drupal 10, services are used throughout the system to perform specific tasks. To use a service, you simply need to use the Drupal class and call the service method, passing in the name of the service you want to use. Here's an example of how you might use the mail.manager service to send an email:

use Drupal\Core\Mail\MailManagerInterface;
/**
* The mail manager service.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* Constructs a new MyModuleMailer object.
*
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
*   The mail manager service.
*/
public function __construct(MailManagerInterface $mail_manager) {
 $this->mailManager = $mail_manager;
}
/**
* Sends an email.
*/
public function sendEmail($to, $subject, $message) {
 $params = [
   'to' => $to,
   'subject' => $subject,
   'message' => $message,
 ];
 $this->mailManager->mail('my_module', 'mail_key', $to, 'en', $params);
}

In this example, we're using the mail.manager service to send an email. The mail method of the mail.manager service is used to send the email, and we're passing in several parameters, including the recipient's email address, the subject of the email, and the message itself.

How do you call them in Drupal 10, and what are the core services ?

In Drupal 9, you can call a service by using the \Drupal class and its service method. Here's an example of how you can use the mail.manager service to send an email:


use Drupal\Core\Mail\MailManagerInterface;

$mail_manager = \Drupal::service('mail.manager');
$message = array(
 'to' => 'recipient@example.com',
 'subject' => 'Example Email Subject',
 'body' => 'Example Email Body',
);
$mail_manager->mail('module_name', 'key_name', $message['to'], 'en', $message);

Here's a list of some of the core services available in Drupal 10 :

  • cache.default: provides caching for your site
  • entity.manager: manages entities within your site
  • field.manager: manages fields for your entities
  • language_manager: manages languages on your site
  • link_generator: generates URLs for your site
  • mail.manager: sends emails from your site
  • path.alias_manager: manages URL aliases for your site
  • state: manages state information for your site
  • string_translation: translates strings within your site
  • user.auth: authenticates users on your site

This is by no means an exhaustive list, and you can find a complete list of core services in the Drupal 9 API documentation.

Conclusion

Services in Drupal 10 provide a powerful way to reuse functionality throughout your site. By using services, you can perform specific tasks in a consistent and reliable way, without having to rewrite the same code over and over again. Whether you're building a custom module or simply using Drupal out of the box, services can be a valuable tool in your development arsenal.
 

Comments

All comments go through moderation, so your comment won't display immediately.