Service collector won't collect child services

Created on 13 September 2022, over 2 years ago
Updated 7 September 2024, 5 months ago

Problem/Motivation

Service collector won't collect child services with no class definition, due to \Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass::processServiceCollectorPass only check matching interface for definition itself, but not checking parent's class.

      // Validate the interface.
      $handler = $container->getDefinition($id);
      if (!is_subclass_of($handler->getClass(), $interface)) {
        throw new LogicException("Service '$id' for consumer '$consumer_id' does not implement $interface.");
      }

Steps to reproduce

Create child service with tag:

my_module.root_handler:
  class: Drupal\MyModule\CustomTranslator
  abstract: true

my_module.parent_handler:
  parent: my_module.root_handler

my_module.child_handler:
  parent: my_module.parent_handler
  tag:
    - { name: string_translator }

The container will throw the following exception when compiling:
Symfony\Component\DependencyInjection\Exception\LogicException: Service 'my_module.child_handler' for consumer 'string_translation' does not implement Drupal\Core\StringTranslation\Translator\TranslatorInterface.
even though Drupal\MyModule\CustomTranslator implement that interface.

Proposed resolution

Make \Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass::processServiceCollectorPass check parent's class for matching interface for child service.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs review

Version

11.0 🔥

Component
Base 

Last updated about 5 hours ago

Created by

🇮🇩Indonesia el7cosmos 🇮🇩 GMT+7

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024