You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Drupal 8 || 9 - Getting info about available services in your Drupal installation
Sometimes you need to know what classes you have available to use as a service in your Drupal installation: either because you don't remember by heart all the services provided by the Drupal core (it's impossible!) or because you come to a project with a lot of custom developments accumulated. In between, there are also all the services available through contributed modules, which you can also use in your implementations. For all these reasons I have compiled some ways to get information about services in a Drupal installation.
This snippet was composed from my current position as Senior Drupal Developer at Digitalist Sweden, one of the biggest companies oriented to Open Source in Europe and specifically focused in Drupal.
You can read all the info related to services from the Drupal core reading the main section in the Drupal.org site: https://api.drupal.org/api/drupal/services/9.4.x, and the core.services.yml file: https://api.drupal.org/api/drupal/core%21core.services.yml/9.4.x. This file will be always available within the core of your Drupal installation, in path: /core/core.services.yml (not the default.services.yml file placed in /sites/default) But it contains info only about Drupal core available services.
By using Drush
The Drupal contrib module Devel provides of some utilities in order to work with services from command line and from GUI within your Drupal Installation. Install devel contrib module and use devel commands for Drush:
And when you install the Drupal contrib devel module, you get a new path in your Drupal installation for review and search from GUI all the related-services: naming, PHP classes and aliases. Just go to /devel/container/service in your local deploy and see the page. This can be a more fast option than using commands in prompt.
$container = \Drupal::getContainer();$kernel = $container->get('kernel');$services = $kernel->getCachedContainerDefinition()['services'];
foreach ($services as $service_id =>$value) {
$service_definition = unserialize($value);
// Put a breakpoint in the next line and review service classes.
$service_definition['class'].
}
By search using Service Tags
Sometimes services may be registered using tags, labels to group services of similar or related utility. In that case we can locate them by associated tags using PHP code.