diff --git a/core/lib/Drupal/Core/Entity/EntityTypeListener.php b/core/lib/Drupal/Core/Entity/EntityTypeListener.php index 23542ac88f..258c9ffa62 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeListener.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeListener.php @@ -93,12 +93,13 @@ public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, ar $storage->onFieldableEntityTypeCreate($entity_type, $field_storage_definitions); } - $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type)); - $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type); if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) { $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions); } + + $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type)); + $this->clearCachedDefinitions(); } /** diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index d6e4895c42..68c63e809f 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -416,8 +416,8 @@ public function onEntityTypeDelete(EntityTypeInterface $entity_type) { * {@inheritdoc} */ public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) { - // When installing an entity type, we have to use the live (in-code) entity - // type and field storage definitions. + // When installing a fieldable entity type, we have to use the provided + // entity type and field storage definitions. $this->entityType = $entity_type; $this->fieldStorageDefinitions = $field_storage_definitions; diff --git a/core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php b/core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php index 37dac651e2..a53aff8a82 100644 --- a/core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php +++ b/core/modules/system/tests/modules/entity_test_update/src/EventSubscriber/EntitySchemaSubscriber.php @@ -52,6 +52,25 @@ public static function getSubscribedEvents() { return static::getEntityTypeEvents(); } + /** + * {@inheritdoc} + */ + public function onEntityTypeCreate(EntityTypeInterface $entity_type) { + // Only add the new base field when a test needs it. + if (!$this->state->get('entity_test_update.install_new_base_field_during_create', FALSE)) { + return; + } + + // Add a new base field when the entity type is created. + $definitions = $this->state->get('entity_test_update.additional_base_field_definitions', []); + $definitions['new_base_field'] = BaseFieldDefinition::create('string') + ->setName('new_base_field') + ->setLabel(new TranslatableMarkup('A new base field')); + $this->state->set('entity_test_update.additional_base_field_definitions', $definitions); + + $this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test_update', $definitions['new_base_field']); + } + /** * {@inheritdoc} */ diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index b4d54412b6..5ede8888fa 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -148,6 +148,28 @@ public function testEntityTypeUpdateWithEntityStorageChange() { } } + /** + * Tests installing an additional base field while installing an entity type. + * + * @covers ::installFieldableEntityType + */ + public function testInstallAdditionalBaseFieldDuringFieldableEntityTypeInstallation() { + $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_update'); + $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_update'); + + // Enable the creation of a new base field during the installation of a + // fieldable entity type. + $this->state->set('entity_test_update.install_new_base_field_during_create', TRUE); + + // Install the entity type and check that the additional base field was also + // installed. + $this->entityDefinitionUpdateManager->installFieldableEntityType($entity_type, $field_storage_definitions); + + // Check whether the 'new_base_field' field has been installed correctly. + $field_storage_definition = $this->entityDefinitionUpdateManager->getFieldStorageDefinition('new_base_field', 'entity_test_update'); + $this->assertNotNull($field_storage_definition); + } + /** * Tests creating a fieldable entity type that doesn't exist in code anymore. * @@ -161,10 +183,8 @@ public function testInstallFieldableEntityTypeWithoutInCodeDefinition() { // code that defines it. $this->deleteEntityType(); - // Rename the base table, update the fieldable entity type and check that - // the table has been renamed. + // Install the entity type and check that its tables have been created. $this->entityDefinitionUpdateManager->installFieldableEntityType($entity_type, $field_storage_definitions); - $this->assertTrue($this->database->schema()->tableExists('entity_test_update'), 'The base table of the entity type has been created.'); }