diff --git a/core/lib/Drupal/Core/Entity/BundleEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/BundleEntityStorageInterface.php index 9ed73c3c18..a776da8e50 100644 --- a/core/lib/Drupal/Core/Entity/BundleEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/BundleEntityStorageInterface.php @@ -5,7 +5,7 @@ /** * A storage that supports subclassed entities. */ -interface SubClassableEntityStorageInterface { +interface BundleEntityStorageInterface { /** * A list of bundle classes. diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 6a5c1bc54f..200afa804d 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -15,7 +15,7 @@ /** * Base class for content entity storage handlers. */ -abstract class ContentEntityStorageBase extends EntityStorageBase implements ContentEntityStorageInterface, DynamicallyFieldableEntityStorageInterface, SubClassableEntityStorageInterface { +abstract class ContentEntityStorageBase extends EntityStorageBase implements ContentEntityStorageInterface, DynamicallyFieldableEntityStorageInterface, BundleEntityStorageInterface { use DeprecatedServicePropertyTrait; /** diff --git a/core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.module b/core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.module index fdb86f809d..517ba9874c 100644 --- a/core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.module +++ b/core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.module @@ -11,4 +11,8 @@ function entity_test_subclass_entity_type_alter(array &$entity_types) { /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */ $entity_types['entity_test']->setStorageClass('\Drupal\entity_test_subclass\EntityTestSubclassStorage'); + if (\Drupal::state()->get('entity_test_subclass_enable_ambigious_entity_types', 0)) { + $entity_types['entity_test_no_label']->setStorageClass('\Drupal\entity_test_subclass\EntityTestSubclassStorage'); + $entity_types['entity_test_no_label']->setClass('\Drupal\entity_test\Entity\EntityTest'); + } } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntitySubclassTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntitySubclassTest.php index ede2dd0bb8..f30c97610b 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntitySubclassTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntitySubclassTest.php @@ -2,6 +2,7 @@ namespace Drupal\KernelTests\Core\Entity; +use Drupal\Core\Entity\Exception\AmbiguousEntityClassException; use Drupal\entity_test_subclass\Entity\EntityTestSubclass; /** @@ -60,4 +61,18 @@ public function testEntitySubclass() { $this->assertTrue($entity instanceof EntityTestSubclass); } + /** + * Verify exceptions if multiple classes implement the same bundle. + */ + public function testAmbigiousClassException() { + $this->container->get('state')->set('entity_test_subclass_enable_ambigious_entity_types', 1); + $this->entityTypeManager->clearCachedDefinitions(); + $this->expectException(AmbiguousEntityClassException::class); + entity_test_create_bundle('subclass'); + + // Since we now have two entity types that returns the same class for the + // same bundle, we expect this to throw an exception. + EntityTestSubclass::create(); + } + }