16

Is there a way to get the list of the available services in Drupal 8?

8 Answers 8

21

to Enhance @4k4 answer, because it returns a lot of services names, if you looking for the specified service name, just pass a part of its name

Drush only method

drush eval "print_r(\Drupal::getContainer()->getServiceIds());"

for instances, if you looking for log services

drush eval "print_r(\Drupal::getContainer()->getServiceIds());" | grep "log"

and if you looking for cache services

drush eval "print_r(\Drupal::getContainer()->getServiceIds());" | grep "cache"

Drush with devel module

Devel module offers a nice shortcut for drush

drush devel:services

or use its aliases:

drush devel-container-services

OR

drush dcs

OR

drush devel-services

Drupal console

drupal debug:container

So drush dcs | grep "PART_OF_SERVICE_NAME" will be a good sample to find a serice

Update

in new version you can just do it with drush dcs PART_OF_SERVICE_NAME

( thanks @Cameron to mention this in comment)

3
  • It takes a parameter so 'drush dcs PART_OF_SERVICE_NAME' works.
    – Cameron
    Commented Feb 4, 2022 at 7:27
  • 1
    @Cameron thanks. I've update the answer with your comment.
    – Yuseferi
    Commented Feb 4, 2022 at 8:50
  • 1
    Drupal console is IMHO not the best answer anymore. Drush can do it without any extra module, and Drush is maintained much better than Drupal console.
    – Hudri
    Commented Nov 6, 2023 at 17:29
23

The module Devel provides a searchable list (/devel/container/service).

For the command line use Drupal Console:

drupal debug:container
9
  • That's perfect ! Thanks for help . Also found another solution we can search it at api.drupal.org/api/drupal/services
    – riju.srk
    Commented Mar 30, 2017 at 12:22
  • Yes, the list online is perfect for core services, but if you search with devel or console locally you'll also get services from installed modules.
    – 4uk4
    Commented Mar 30, 2017 at 12:34
  • I agree completely,actually I think looking with Devel is better because may be some contributed module provide some service which is not listed online.
    – riju.srk
    Commented Mar 31, 2017 at 13:47
  • It's too bad the devel list of services doesn't include the methods of those services. :(
    – liquidcms
    Commented Apr 23, 2020 at 23:35
  • 2
    Or since Drupal Console does not seem to be active anymore, you can use drush to get this information. Make sure the devel module is installed and then run drush devel:services.
    – bkudrle
    Commented Sep 24, 2021 at 18:01
7

With plain drush, no extra modules required:

drush eval "print_r(\Drupal::getContainer()->getServiceIds());"

If you are searching for a something specific, pipe the output into grep to filter the results, e.g. searching for "views"

drush eval "print_r(\Drupal::getContainer()->getServiceIds());" | grep "views"

4

\Drupal::getContainer()->getServiceIds() is the REPL friendly way of doing this. Also you can use that on your module code etc..

The services can be also filtered:

preg_grep('/cache/', \Drupal::getContainer()->getServiceIds());
2

A list of services are available on drupal.org https://api.drupal.org/api/drupal/services/8.9.x

2

If you are interested to see the available services during development in PHPStorm you can enable the SymfonyPlugin which autocompletes for you the service names (and does much more other handy stuff)

It works both in services.yml and when calling $container->get(...).

enter image description here

3
  • 1
    Additionally in order for this to work properly you need to set your project as "Drupal project" on PHPStorm. Usually "he" is smart enough to suggest it to you when you create the project the first time. Check this jetbrains.com/help/phpstorm/drupal-support.html to find out more about Drupal's support on PHPStorm.
    – d70rr3s
    Commented Jan 20, 2020 at 14:46
  • Unfortunately, it seems one cannot stop the animation. It can get a bit annoying.
    – Geoffrey
    Commented Mar 11, 2020 at 4:02
  • I will put it in spoiler.
    – ssibal
    Commented Mar 11, 2020 at 8:51
0

There is the PHPStorm Metadata module which can be used to get autocompletion for all services (core, contrib and custom) in PHPStorm. And it also can give you that list.

This module allows to export PHPStorm Advanced Metadata in order to get autocomplete functionality and return type information for next calls:

  • Drupal::service()
  • ContainerInterface::get()
  • EntityTypeManager::getStorage()

After installing and enabling it, run drush phpstorm-metadata:generate and it will generate a file called .phpstorm.meta.php in your Drupal root directory. Inside there is a list of all available services (core, contrib and custom) mapped to their class.

// ...
'theme_handler' =>  \Drupal\Core\Extension\ThemeHandler::class,
'theme_installer' =>  \Drupal\Core\Extension\ThemeInstaller::class,
'entity.manager' =>  \Drupal\Core\Entity\EntityManager::class,
'entity.memory_cache' =>  \Drupal\Core\Cache\MemoryCache\MemoryCache::class,
'entity_type.manager' =>  \Drupal\Core\Entity\EntityTypeManager::class,
'entity_type.repository' =>  \Drupal\Core\Entity\EntityTypeRepository::class,
'entity_type.bundle.info' =>  \Drupal\Core\Entity\EntityTypeBundleInfo::class,
'entity.repository' =>  \Drupal\Core\Entity\EntityRepository::class,
// ...
-1

Since Drupal 8 used Symfony so you can run command php vendor/bin/drupal debug:container and this is equivalent in Symfony php bin/console debug:container

1
  • 1
    This isn't necessarily true. I would modify the post to add that drupal-console is required.
    – mradcliffe
    Commented May 15, 2020 at 13:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.