Loading core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +6 −2 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; use Drupal\Core\Entity\Exception\AmbiguousBundleClassException; use Drupal\Core\Entity\Exception\BundleClassInheritanceException; use Drupal\Core\Entity\Exception\MissingBundleClassException; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Language\LanguageInterface; Loading Loading @@ -202,9 +203,12 @@ public function getEntityClass(?string $bundle = NULL): string { $bundle_info = $this->entityTypeBundleInfo->getBundleInfo($this->entityTypeId); $bundle_class = $bundle_info[$bundle]['class'] ?? NULL; // Bundle classes should extend the main entity class. // Bundle classes should exist and extend the main entity class. if ($bundle_class) { if (!is_subclass_of($bundle_class, $entity_class)) { if (!class_exists($bundle_class)) { throw new MissingBundleClassException($bundle_class); } elseif (!is_subclass_of($bundle_class, $entity_class)) { throw new BundleClassInheritanceException($bundle_class, $entity_class); } return $bundle_class; Loading core/lib/Drupal/Core/Entity/Exception/MissingBundleClassException.php 0 → 100644 +23 −0 Original line number Diff line number Diff line <?php namespace Drupal\Core\Entity\Exception; /** * Exception thrown if a bundle class does not exist. * * @see \Drupal\Core\Entity\ContentEntityStorageBase::getEntityClass() */ class MissingBundleClassException extends \Exception { /** * Constructs a MissingBundleClassException. * * @param string $bundle_class * The bundle class which should exist. */ public function __construct(string $bundle_class) { $message = sprintf('Bundle class %s does not exist.', $bundle_class); parent::__construct($message); } } core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.module +4 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,10 @@ function entity_test_bundle_class_entity_bundle_info_alter(&$bundles) { if (\Drupal::state()->get('entity_test_bundle_class_enable_user_class', FALSE)) { $bundles['user']['user']['class'] = EntityTestUserClass::class; } if (\Drupal::state()->get('entity_test_bundle_class_does_not_exist', FALSE)) { $bundles['entity_test']['bundle_class']['class'] = '\Drupal\Core\NonExistentClass'; } } /** Loading core/tests/Drupal/KernelTests/Core/Entity/BundleClassTest.php +12 −0 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ use Drupal\Core\Entity\Exception\AmbiguousBundleClassException; use Drupal\Core\Entity\Exception\BundleClassInheritanceException; use Drupal\Core\Entity\Exception\MissingBundleClassException; use Drupal\entity_test\Entity\EntityTest; use Drupal\entity_test_bundle_class\Entity\EntityTestAmbiguousBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestBundleClass; Loading Loading @@ -249,6 +250,17 @@ public function testBundleClassShouldExtendEntityClass() { $this->storage->create(['type' => 'bundle_class']); } /** * Checks exception thrown if a bundle class doesn't exist. */ public function testBundleClassShouldExist() { $this->container->get('state')->set('entity_test_bundle_class_does_not_exist', TRUE); $this->entityTypeManager->clearCachedDefinitions(); $this->expectException(MissingBundleClassException::class); entity_test_create_bundle('bundle_class'); $this->storage->create(['type' => 'bundle_class']); } /** * Tests that a module can override an entity-type class. * Loading Loading
core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +6 −2 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; use Drupal\Core\Entity\Exception\AmbiguousBundleClassException; use Drupal\Core\Entity\Exception\BundleClassInheritanceException; use Drupal\Core\Entity\Exception\MissingBundleClassException; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Language\LanguageInterface; Loading Loading @@ -202,9 +203,12 @@ public function getEntityClass(?string $bundle = NULL): string { $bundle_info = $this->entityTypeBundleInfo->getBundleInfo($this->entityTypeId); $bundle_class = $bundle_info[$bundle]['class'] ?? NULL; // Bundle classes should extend the main entity class. // Bundle classes should exist and extend the main entity class. if ($bundle_class) { if (!is_subclass_of($bundle_class, $entity_class)) { if (!class_exists($bundle_class)) { throw new MissingBundleClassException($bundle_class); } elseif (!is_subclass_of($bundle_class, $entity_class)) { throw new BundleClassInheritanceException($bundle_class, $entity_class); } return $bundle_class; Loading
core/lib/Drupal/Core/Entity/Exception/MissingBundleClassException.php 0 → 100644 +23 −0 Original line number Diff line number Diff line <?php namespace Drupal\Core\Entity\Exception; /** * Exception thrown if a bundle class does not exist. * * @see \Drupal\Core\Entity\ContentEntityStorageBase::getEntityClass() */ class MissingBundleClassException extends \Exception { /** * Constructs a MissingBundleClassException. * * @param string $bundle_class * The bundle class which should exist. */ public function __construct(string $bundle_class) { $message = sprintf('Bundle class %s does not exist.', $bundle_class); parent::__construct($message); } }
core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.module +4 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,10 @@ function entity_test_bundle_class_entity_bundle_info_alter(&$bundles) { if (\Drupal::state()->get('entity_test_bundle_class_enable_user_class', FALSE)) { $bundles['user']['user']['class'] = EntityTestUserClass::class; } if (\Drupal::state()->get('entity_test_bundle_class_does_not_exist', FALSE)) { $bundles['entity_test']['bundle_class']['class'] = '\Drupal\Core\NonExistentClass'; } } /** Loading
core/tests/Drupal/KernelTests/Core/Entity/BundleClassTest.php +12 −0 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ use Drupal\Core\Entity\Exception\AmbiguousBundleClassException; use Drupal\Core\Entity\Exception\BundleClassInheritanceException; use Drupal\Core\Entity\Exception\MissingBundleClassException; use Drupal\entity_test\Entity\EntityTest; use Drupal\entity_test_bundle_class\Entity\EntityTestAmbiguousBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestBundleClass; Loading Loading @@ -249,6 +250,17 @@ public function testBundleClassShouldExtendEntityClass() { $this->storage->create(['type' => 'bundle_class']); } /** * Checks exception thrown if a bundle class doesn't exist. */ public function testBundleClassShouldExist() { $this->container->get('state')->set('entity_test_bundle_class_does_not_exist', TRUE); $this->entityTypeManager->clearCachedDefinitions(); $this->expectException(MissingBundleClassException::class); entity_test_create_bundle('bundle_class'); $this->storage->create(['type' => 'bundle_class']); } /** * Tests that a module can override an entity-type class. * Loading