diff --git a/core/lib/Drupal/Core/Entity/EntityTypeRepository.php b/core/lib/Drupal/Core/Entity/EntityTypeRepository.php index 63e7fee1a5..5930a249d1 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeRepository.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeRepository.php @@ -2,6 +2,7 @@ namespace Drupal\Core\Entity; +use Drupal\Core\Entity\Exception\AmbiguousBundleClassException; use Drupal\Core\Entity\Exception\AmbiguousEntityClassException; use Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -80,17 +81,9 @@ public function getEntityTypeFromClass($class_name) { $same_class = 0; $entity_type_id = NULL; - foreach ($this->entityTypeManager->getDefinitions() as $entity_type) { - if ( - // The class name can be defined by the module that provides the entity. - $entity_type->getOriginalClass() === $class_name || - // It can be an entity class overridden by a different module. - $entity_type->getClass() === $class_name || - // It can be a bundle class that extends the original entity class. - is_subclass_of($class_name, $entity_type->getOriginalClass()) || - // Or it can be a bundle class that extends an overridden entity class. - is_subclass_of($class_name, $entity_type->getClass()) - ) { + $definitions = $this->entityTypeManager->getDefinitions(); + foreach ($definitions as $entity_type) { + if ($entity_type->getOriginalClass() == $class_name || $entity_type->getClass() == $class_name) { $entity_type_id = $entity_type->id(); if ($same_class++) { throw new AmbiguousEntityClassException($class_name); @@ -98,6 +91,20 @@ public function getEntityTypeFromClass($class_name) { } } + // If no match was found check if it is a bundle class. This needs to be in + // a separate loop to avoid false positives, since an entity class can + // subclass another entity class. + if (!$entity_type_id) { + foreach ($definitions as $entity_type) { + if (is_subclass_of($class_name, $entity_type->getOriginalClass()) || is_subclass_of($class_name, $entity_type->getClass())) { + $entity_type_id = $entity_type->id(); + if ($same_class++) { + throw new AmbiguousBundleClassException($class_name); + } + } + } + } + // Return the matching entity type ID if there is one. if ($entity_type_id) { $this->classNameEntityTypeMap[$class_name] = $entity_type_id; diff --git a/core/lib/Drupal/Core/Entity/EntityTypeRepositoryInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeRepositoryInterface.php index 80b96e477a..5f4c2ccf15 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeRepositoryInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeRepositoryInterface.php @@ -34,6 +34,8 @@ public function getEntityTypeLabels($group = FALSE); * * @throws \Drupal\Core\Entity\Exception\AmbiguousEntityClassException * Thrown when multiple subclasses correspond to the called class. + * @throws \Drupal\Core\Entity\Exception\AmbiguousBundleClassException + * Thrown when multiple subclasses correspond to the called bundle class. * @throws \Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException * Thrown when no entity class corresponds to the called class. *