AmbiguousBundleClassException if multiple entity types share the same class

Created on 7 January 2022, over 2 years ago
Updated 15 May 2024, 18 days ago

Problem/Motivation

The EntityTypeRepository checks that there is only one class per entity type. However, in contrib such as ECK, entity bundle classes are generated dynamically from configuration with the module not able to dynamically create classes per configured bundle. This means that the class itself must be the same as PHP does not support Generics. So creating two bundles derived from the same ECK entity currently throws an AmbiguousEntityClassException.

EntityTypeRepositoryInterface::getEntityTypeFromClass triggers this issue. The workaround to manually create a class for every ECK entity type renders the module unusable, but even if it were to be possible, it would still fail on this line (is_subclass_of($class_name, $entity_type->getOriginalClass())) because the original class is still Drupal\eck\Entity\EckEntity, regardless of any overrides.

The code in question was introduced in #2570593 .

Steps to reproduce

  1. Install the eck module
  2. Create two eck entity types + entity type classes subclassing EckEntity, and register them using hook_entity_type_alter
  3. Create a bundle on one of those entity types + a class for that bundle, and register it using hook_entity_bundle_info_alter
  4. Call EntityTypeRepository::getEntityTypeFromClass() with the bundle class name.

Proposed resolution

Require passing entity bundle info to the EntityTypeRepository to avoid ambiguous calls when there are two or more bundles for the same entity class.

🐛 Bug report
Status

Fixed

Version

10.3

Component
Entity 

Last updated 1 day ago

Created by

🇧🇪Belgium DieterHolvoet Brussels

Live updates comments and jobs are added and updated live.

Missing content requested by

🇦🇺Australia dpi
5 months ago
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024