Is there an easy (supported) way to programatically get all services in Drupal 8 that are tagged with a specific tag?

For example getting all services that are tagged with "breadcrumb_builder" tag

Example for a service tag from book module:

    class: Drupal\book\BookBreadcrumbBuilder
    arguments: ['@entity_type.manager', '@current_user']
      - { name: breadcrumb_builder, priority: 701 }

Now if i want to get all services from all (enabled) *.services.yml files that are tagged with "breadcrumb_builder"?

I have seen and read this: https://symfony.com/doc/3.4/service_container/tags.html

But this seems to be really hard work and I am not sure if its even possible that way without touching Drupal core.

The Reference Tagged Services method looked very promising at first, but seems not to be supported from Drupal by default.

  • Can't you use the $container->findTaggedServiceIds('tag_name');? – user72672 Oct 16 '19 at 15:14
  • You need to create a custom compiler pass, tags are only available at compile time – Clive Oct 16 '19 at 15:25
  • Or put your own service_collector service in the container which collects the tagged services at compile time. It's unclear what this question is about, but if you want to use the collected services for something this would be the way to go. – 4k4 Oct 16 '19 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.