diff --git a/core/lib/Drupal/Core/Entity/BundleEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/BundleEntityStorageInterface.php index 4d9c4e382f..9ba915fabd 100644 --- a/core/lib/Drupal/Core/Entity/BundleEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/BundleEntityStorageInterface.php @@ -3,7 +3,7 @@ namespace Drupal\Core\Entity; /** - * A storage that supports subclassed entities. + * A storage that supports entities with bundle specific classes. */ interface BundleEntityStorageInterface { diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 56ce7cd6db..e1e0395a7c 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -1134,7 +1134,7 @@ public static function create(array $values = []) { $class_name = get_called_class(); $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass($class_name)); - // Always explicitly specify the bundle for a subclassed entity. + // Always explicitly specify the bundle if the entity has a bundle class. if ($storage instanceof ContentEntityStorageBase && ($bundle = $storage->getBundleFromClass($class_name))) { $values[$storage->getBundleKey()] = $bundle; } diff --git a/core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.info.yml b/core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.info.yml similarity index 47% rename from core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.info.yml rename to core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.info.yml index f6c5769eb6..f295db0241 100644 --- a/core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.info.yml +++ b/core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.info.yml @@ -1,6 +1,6 @@ -name: 'Entity Subclass Test' +name: 'Entity Bundle Class Test' type: module -description: 'Support module for testing entity type subclassing.' +description: 'Support module for testing entity bundle classes.' package: Testing version: VERSION core: 8.x diff --git a/core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.module b/core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.module similarity index 18% rename from core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.module rename to core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.module index b075c7d521..576143a4c1 100644 --- a/core/modules/system/tests/modules/entity_test_subclass/entity_test_subclass.module +++ b/core/modules/system/tests/modules/entity_test_bundle_class/entity_test_bundle_class.module @@ -2,25 +2,25 @@ /** * @file - * Support module for testing entity type subclassing. + * Support module for testing entity bundle classes. */ -use Drupal\entity_test_subclass\Entity\EntityTestSubclass; -use Drupal\entity_test_subclass\Entity\NonInheritingSubclass; +use Drupal\entity_test_bundle_class\Entity\EntityTestBundleClass; +use Drupal\entity_test_bundle_class\Entity\NonInheritingBundleClass; /** * Implements hook_entity_bundle_info_alter(). */ -function entity_test_subclass_entity_bundle_info_alter(&$bundles) { - if (!empty($bundles['entity_test']['subclass'])) { - $bundles['entity_test']['subclass']['class'] = EntityTestSubclass::class; +function entity_test_bundle_class_entity_bundle_info_alter(&$bundles) { + if (!empty($bundles['entity_test']['bundle_class'])) { + $bundles['entity_test']['bundle_class']['class'] = EntityTestBundleClass::class; } - if (\Drupal::state()->get('entity_test_subclass_enable_ambiguous_entity_types', FALSE)) { - $bundles['entity_test']['entity_test_no_label']['class'] = EntityTestSubclass::class; + if (\Drupal::state()->get('entity_test_bundle_class_enable_ambiguous_entity_types', FALSE)) { + $bundles['entity_test']['entity_test_no_label']['class'] = EntityTestBundleClass::class; } - if (\Drupal::state()->get('entity_test_subclass_non_inheriting', FALSE)) { - $bundles['entity_test']['subclass']['class'] = NonInheritingSubclass::class; + if (\Drupal::state()->get('entity_test_bundle_class_non_inheriting', FALSE)) { + $bundles['entity_test']['bundle_class']['class'] = NonInheritingBundleClass::class; } } diff --git a/core/modules/system/tests/modules/entity_test_subclass/src/Entity/EntityTestSubclass.php b/core/modules/system/tests/modules/entity_test_bundle_class/src/Entity/EntityTestBundleClass.php similarity index 25% rename from core/modules/system/tests/modules/entity_test_subclass/src/Entity/EntityTestSubclass.php rename to core/modules/system/tests/modules/entity_test_bundle_class/src/Entity/EntityTestBundleClass.php index c469d280f0..56291da832 100644 --- a/core/modules/system/tests/modules/entity_test_subclass/src/Entity/EntityTestSubclass.php +++ b/core/modules/system/tests/modules/entity_test_bundle_class/src/Entity/EntityTestBundleClass.php @@ -1,11 +1,11 @@ assertTrue($entity instanceof EntityTestSubclass); + // Verify statically created entity with bundle class returns correct class. + $entity = EntityTestBundleClass::create(); + $this->assertTrue($entity instanceof EntityTestBundleClass); - // Verify statically created subclassed entity returns correct bundle. - $entity = EntityTestSubclass::create(['type' => 'custom']); - $this->assertTrue($entity instanceof EntityTestSubclass); - $this->assertEquals('subclass', $entity->bundle()); + // Verify statically created entity with bundle class returns correct + // bundle. + $entity = EntityTestBundleClass::create(['type' => 'custom']); + $this->assertTrue($entity instanceof EntityTestBundleClass); + $this->assertEquals('bundle_class', $entity->bundle()); // Verify that the entity storage creates the entity using the proper class. - $entity = $this->storage->create(['type' => 'subclass']); - $this->assertTrue($entity instanceof EntityTestSubclass); + $entity = $this->storage->create(['type' => 'bundle_class']); + $this->assertTrue($entity instanceof EntityTestBundleClass); // Verify that loading an entity returns the proper class. $entity->save(); $id = $entity->id(); $this->storage->resetCache(); $entity = $this->storage->load($id); - $this->assertTrue($entity instanceof EntityTestSubclass); + $this->assertTrue($entity instanceof EntityTestBundleClass); } /** * Checks exception is thrown if multiple classes implement the same bundle. */ public function testAmbiguousBundleClassException() { - $this->container->get('state')->set('entity_test_subclass_enable_ambiguous_entity_types', TRUE); + $this->container->get('state')->set('entity_test_bundle_class_enable_ambiguous_entity_types', TRUE); $this->entityTypeManager->clearCachedDefinitions(); $this->expectException(AmbiguousBundleClassException::class); - entity_test_create_bundle('subclass'); + entity_test_create_bundle('bundle_class'); // 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(); + EntityTestBundleClass::create(); } /** * Checks exception thrown if a bundle class doesn't extend the entity class. */ public function testBundleClassShouldExtendEntityClass() { - $this->container->get('state')->set('entity_test_subclass_non_inheriting', TRUE); + $this->container->get('state')->set('entity_test_bundle_class_non_inheriting', TRUE); $this->entityTypeManager->clearCachedDefinitions(); $this->expectException(BundleClassInheritanceException::class); - entity_test_create_bundle('subclass'); - $this->storage->create(['type' => 'subclass']); + entity_test_create_bundle('bundle_class'); + $this->storage->create(['type' => 'bundle_class']); } }