Loading core/lib/Drupal/Core/Entity/EntityStorageBase.php +9 −10 Original line number Diff line number Diff line Loading @@ -60,16 +60,16 @@ abstract class EntityStorageBase extends EntityHandlerBase implements EntityStor protected $uuidService; /** * Name of the entity class (if set directly via deprecated means). * Name of the base entity class. * * This is a private property since it's not meant to be set by child classes. * It holds the name of the entity class defined in the entity type that is * passed in to the constructor when instantiating an entity storage class. * * This is a private property since it's only here to support backwards * compatibility for deprecated code paths in contrib and custom code. * Normally, the entity class is defined via an annotation when defining an * entity type, via hook_entity_bundle_info() or via * hook_entity_bundle_info_alter(). * * @todo Remove this in Drupal 10. * @see https://www.drupal.org/project/drupal/issues/3244802 * hook_entity_bundle_info_alter(). However, due to how this property works, * the entity class can also be controlled via hook_entity_type_alter(). * * @var string|null */ Loading Loading @@ -100,6 +100,7 @@ abstract class EntityStorageBase extends EntityHandlerBase implements EntityStor public function __construct(EntityTypeInterface $entity_type, MemoryCacheInterface $memory_cache) { $this->entityTypeId = $entity_type->id(); $this->entityType = $entity_type; $this->baseEntityClass = $entity_type->getClass(); $this->idKey = $this->entityType->getKey('id'); $this->uuidKey = $this->entityType->getKey('uuid'); $this->langcodeKey = $this->entityType->getKey('langcode'); Loading @@ -111,9 +112,7 @@ public function __construct(EntityTypeInterface $entity_type, MemoryCacheInterfa * {@inheritdoc} */ public function getEntityClass(?string $bundle = NULL): string { // @todo Simplify this in Drupal 10 to return $this->entityType->getClass(). // @see https://www.drupal.org/project/drupal/issues/3244802 return $this->baseEntityClass ?? $this->entityType->getClass(); return $this->baseEntityClass; } /** Loading core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.module +11 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ use Drupal\entity_test_bundle_class\Entity\EntityTestAmbiguousBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestUserClass; use Drupal\entity_test_bundle_class\Entity\EntityTestVariant; use Drupal\entity_test_bundle_class\Entity\NonInheritingBundleClass; /** Loading @@ -32,3 +33,13 @@ function entity_test_bundle_class_entity_bundle_info_alter(&$bundles) { $bundles['user']['user']['class'] = EntityTestUserClass::class; } } /** * Implements hook_entity_type_alter(). */ function entity_test_bundle_class_entity_type_alter(&$entity_types) { /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */ if (\Drupal::state()->get('entity_test_bundle_class_override_base_class', FALSE) && isset($entity_types['entity_test'])) { $entity_types['entity_test']->setClass(EntityTestVariant::class); } } core/modules/system/tests/modules/entity_test_bundle_class/src/Entity/EntityTestVariant.php 0 → 100644 +12 −0 Original line number Diff line number Diff line <?php namespace Drupal\entity_test_bundle_class\Entity; use Drupal\entity_test\Entity\EntityTest; /** * Defines a custom class to use for EntityTest. * * @see entity_test_bundle_class_entity_type_alter() */ class EntityTestVariant extends EntityTest {} core/modules/system/tests/modules/entity_test_deprecated_storage/src/Storage/DeprecatedEntityStorage.php +3 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,9 @@ /** * Class for testing deprecation warnings from EntityStorageBase. * * @todo Remove this in Drupal 10. * @see https://www.drupal.org/project/drupal/issues/3244802 */ class DeprecatedEntityStorage extends SqlContentEntityStorage { Loading core/tests/Drupal/KernelTests/Core/Entity/BundleClassTest.php +14 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ use Drupal\entity_test_bundle_class\Entity\EntityTestAmbiguousBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestUserClass; use Drupal\entity_test_bundle_class\Entity\EntityTestVariant; use Drupal\user\Entity\User; /** Loading Loading @@ -248,4 +249,17 @@ public function testBundleClassShouldExtendEntityClass() { $this->storage->create(['type' => 'bundle_class']); } /** * Tests that a module can override an entity-type class. * * Ensures a module can implement hook_entity_info_alter() and alter the * entity's class without needing to write to the last installed * definitions repository. */ public function testEntityClassNotTakenFromActiveDefinitions(): void { $this->container->get('state')->set('entity_test_bundle_class_override_base_class', TRUE); $this->entityTypeManager->clearCachedDefinitions(); $this->assertEquals(EntityTestVariant::class, $this->entityTypeManager->getStorage('entity_test')->getEntityClass()); } } Loading
core/lib/Drupal/Core/Entity/EntityStorageBase.php +9 −10 Original line number Diff line number Diff line Loading @@ -60,16 +60,16 @@ abstract class EntityStorageBase extends EntityHandlerBase implements EntityStor protected $uuidService; /** * Name of the entity class (if set directly via deprecated means). * Name of the base entity class. * * This is a private property since it's not meant to be set by child classes. * It holds the name of the entity class defined in the entity type that is * passed in to the constructor when instantiating an entity storage class. * * This is a private property since it's only here to support backwards * compatibility for deprecated code paths in contrib and custom code. * Normally, the entity class is defined via an annotation when defining an * entity type, via hook_entity_bundle_info() or via * hook_entity_bundle_info_alter(). * * @todo Remove this in Drupal 10. * @see https://www.drupal.org/project/drupal/issues/3244802 * hook_entity_bundle_info_alter(). However, due to how this property works, * the entity class can also be controlled via hook_entity_type_alter(). * * @var string|null */ Loading Loading @@ -100,6 +100,7 @@ abstract class EntityStorageBase extends EntityHandlerBase implements EntityStor public function __construct(EntityTypeInterface $entity_type, MemoryCacheInterface $memory_cache) { $this->entityTypeId = $entity_type->id(); $this->entityType = $entity_type; $this->baseEntityClass = $entity_type->getClass(); $this->idKey = $this->entityType->getKey('id'); $this->uuidKey = $this->entityType->getKey('uuid'); $this->langcodeKey = $this->entityType->getKey('langcode'); Loading @@ -111,9 +112,7 @@ public function __construct(EntityTypeInterface $entity_type, MemoryCacheInterfa * {@inheritdoc} */ public function getEntityClass(?string $bundle = NULL): string { // @todo Simplify this in Drupal 10 to return $this->entityType->getClass(). // @see https://www.drupal.org/project/drupal/issues/3244802 return $this->baseEntityClass ?? $this->entityType->getClass(); return $this->baseEntityClass; } /** Loading
core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.module +11 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ use Drupal\entity_test_bundle_class\Entity\EntityTestAmbiguousBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestUserClass; use Drupal\entity_test_bundle_class\Entity\EntityTestVariant; use Drupal\entity_test_bundle_class\Entity\NonInheritingBundleClass; /** Loading @@ -32,3 +33,13 @@ function entity_test_bundle_class_entity_bundle_info_alter(&$bundles) { $bundles['user']['user']['class'] = EntityTestUserClass::class; } } /** * Implements hook_entity_type_alter(). */ function entity_test_bundle_class_entity_type_alter(&$entity_types) { /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */ if (\Drupal::state()->get('entity_test_bundle_class_override_base_class', FALSE) && isset($entity_types['entity_test'])) { $entity_types['entity_test']->setClass(EntityTestVariant::class); } }
core/modules/system/tests/modules/entity_test_bundle_class/src/Entity/EntityTestVariant.php 0 → 100644 +12 −0 Original line number Diff line number Diff line <?php namespace Drupal\entity_test_bundle_class\Entity; use Drupal\entity_test\Entity\EntityTest; /** * Defines a custom class to use for EntityTest. * * @see entity_test_bundle_class_entity_type_alter() */ class EntityTestVariant extends EntityTest {}
core/modules/system/tests/modules/entity_test_deprecated_storage/src/Storage/DeprecatedEntityStorage.php +3 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,9 @@ /** * Class for testing deprecation warnings from EntityStorageBase. * * @todo Remove this in Drupal 10. * @see https://www.drupal.org/project/drupal/issues/3244802 */ class DeprecatedEntityStorage extends SqlContentEntityStorage { Loading
core/tests/Drupal/KernelTests/Core/Entity/BundleClassTest.php +14 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ use Drupal\entity_test_bundle_class\Entity\EntityTestAmbiguousBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestBundleClass; use Drupal\entity_test_bundle_class\Entity\EntityTestUserClass; use Drupal\entity_test_bundle_class\Entity\EntityTestVariant; use Drupal\user\Entity\User; /** Loading Loading @@ -248,4 +249,17 @@ public function testBundleClassShouldExtendEntityClass() { $this->storage->create(['type' => 'bundle_class']); } /** * Tests that a module can override an entity-type class. * * Ensures a module can implement hook_entity_info_alter() and alter the * entity's class without needing to write to the last installed * definitions repository. */ public function testEntityClassNotTakenFromActiveDefinitions(): void { $this->container->get('state')->set('entity_test_bundle_class_override_base_class', TRUE); $this->entityTypeManager->clearCachedDefinitions(); $this->assertEquals(EntityTestVariant::class, $this->entityTypeManager->getStorage('entity_test')->getEntityClass()); } }