Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidjguru/093ec6bbab248697091129757b8e6046 to your computer and use it in GitHub Desktop.
Save davidjguru/093ec6bbab248697091129757b8e6046 to your computer and use it in GitHub Desktop.
Drupal 8 || 9 - Getting info about available services in your Drupal installation

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.

Author

Acknowledgments

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.

By searching Drupal Services on the Drupal API

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:

$ drush devel:services
$ drush devel-container-services
$ drush dcs
$ drush devel-services

Install and use devel commands:

$ composer require drupal/devel
$ drush en -y devel
$ drush cr 
$ drush devel:services

    - access_arguments_resolver_factory
    - access_check.cron
    - access_check.csrf
    - access_check.custom
    - access_check.db_update
    [...]
    - views.executable
    - views.exposed_form_cache
    - views.route_subscriber
    - views.views_data
    - views.views_data_helper
    - watchdog.commands

Count how many services are in your Drupal installation:

$ lando drush dcs | wc -l

  828

Getting info about services using prefixes:

$ drush dcs plugin.manager
  
 - plugin.manager.action
 - plugin.manager.alias_type
 - plugin.manager.archiver
 - plugin.manager.block
 - plugin.manager.ckeditor.plugin
 - plugin.manager.condition
 - plugin.manager.config_filter
 - plugin.manager.config_translation.mapper
 - plugin.manager.core.layout
 - plugin.manager.crop.entity_provider
  [...]

Same as:

$ lando drush dcs | grep breadcrumb

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.

By code

For debugging purposes you can use the getCachedContainerDefinition function from the DrupalKernel class.

$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.

services:
  my.breadcrumb:
    class: Drupal\my_custom\MyBreadcrumbBuilder
    arguments: ['@entity_type.manager', '@current_user']
    tags:
      - { name: breadcrumb_builder, priority: 701 }

So you can get results requesting services by tag from a Controller class:

//use Symfony\Component\DependencyInjection\ContainerBuilder;
public function process(ContainerBuilder $container) {
    $taggedServices = $container->findTaggedServiceIds('breadcrumb_builder');
}

By search service files using Linux

We can search service files using the command "find" available in Linux:

$ find ./web/modules/ -name "*.services.yml"
  ./web/modules/contrib/media_library_form_element/media_library_form_element.services.yml
  ./web/modules/contrib/entity/tests/modules/entity_module_bundle_plugin_test/entity_module_bundle_plugin_test.services.yml
  ./web/modules/contrib/entity/tests/modules/entity_module_test/entity_module_test.services.yml
  ./web/modules/contrib/entity/entity.services.yml
  ./web/modules/contrib/admin_toolbar/admin_toolbar_search/admin_toolbar_search.services.yml
  ./web/modules/contrib/admin_toolbar/admin_toolbar_tools/admin_toolbar_tools.services.yml
  ./web/modules/contrib/memcache/memcache_admin/memcache_admin.services.yml
[...]

Or counting how many service files are in the /modules zone:

$ find ./web/modules/ -name "*.services.yml" | wc -l
  76

Read More about Drupal Services

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment