Is there a way to get the list of the available services in Drupal 8?
8 Answers
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)
The module Devel provides a searchable list (/devel/container/service).
For the command line use Drupal Console:
drupal debug:container
-
That's perfect ! Thanks for help . Also found another solution we can search it at api.drupal.org/api/drupal/services– riju.srkCommented 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.– 4uk4Commented 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.srkCommented Mar 31, 2017 at 13:47
-
1It's too bad the devel list of services doesn't include the methods of those services. :( Commented Apr 23, 2020 at 23:35
-
2Or 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.– bkudrleCommented Sep 24, 2021 at 18:01
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"
\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());
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(...).
-
1Additionally 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.– d70rr3sCommented Jan 20, 2020 at 14:46
-
Unfortunately, it seems one cannot stop the animation. It can get a bit annoying.– GeoffreyCommented Mar 11, 2020 at 4:02
-
A list of services are available on drupal.org https://api.drupal.org/api/drupal/services/8.9.x
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, // ...
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
-
1This isn't necessarily true. I would modify the post to add that drupal-console is required. Commented May 15, 2020 at 13:18