diff --git a/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php b/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php index 6ef363cf2d1bc2c78d614999613e5b9be681e19d..e67629b75932beb6e2829d3361585231995ed7df 100644 --- a/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php +++ b/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityStorageInterface; +use Drupal\Core\Entity\Sql\SqlContentEntityStorage; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -81,7 +82,16 @@ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $ // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. - $storage = $this->entityTypeManager->getStorage($entity_type_id); + $storage = clone $this->entityTypeManager->getStorage($entity_type_id); + + // Entity type definition updates can change the schema by adding or + // removing entity tables (for example when switching an entity type from + // non-revisionable to revisionable), so CRUD operations on a field storage + // definition need to use the last installed entity type schema. + if ($storage instanceof SqlContentEntityStorage + && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) { + $storage->setEntityType($last_installed_entity_type); + } if ($storage instanceof FieldStorageDefinitionListenerInterface) { $storage->onFieldStorageDefinitionCreate($storage_definition); @@ -101,7 +111,16 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $ // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. - $storage = $this->entityTypeManager->getStorage($entity_type_id); + $storage = clone $this->entityTypeManager->getStorage($entity_type_id); + + // Entity type definition updates can change the schema by adding or + // removing entity tables (for example when switching an entity type from + // non-revisionable to revisionable), so CRUD operations on a field storage + // definition need to use the last installed entity type schema. + if ($storage instanceof SqlContentEntityStorage + && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) { + $storage->setEntityType($last_installed_entity_type); + } if ($storage instanceof FieldStorageDefinitionListenerInterface) { $storage->onFieldStorageDefinitionUpdate($storage_definition, $original); @@ -121,7 +140,16 @@ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $ // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. - $storage = $this->entityTypeManager->getStorage($entity_type_id); + $storage = clone $this->entityTypeManager->getStorage($entity_type_id); + + // Entity type definition updates can change the schema by adding or + // removing entity tables (for example when switching an entity type from + // non-revisionable to revisionable), so CRUD operations on a field storage + // definition need to use the last installed entity type schema. + if ($storage instanceof SqlContentEntityStorage + && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) { + $storage->setEntityType($last_installed_entity_type); + } // Keep the field definition in the deleted fields repository so we can use // it later during field_purge_batch(), but only if the field has data. diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index f33d6a3ce87d1f22805e899eca4fcb01ca5c0552..597d22fb7b7118c67546a3e2687df0d8bc35c393 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -676,6 +676,25 @@ public function testBundleFieldUpdateWithExistingData() { } } + /** + * Tests updating a bundle field when the entity type schema has changed. + */ + public function testBundleFieldUpdateWithEntityTypeSchemaUpdate() { + // Add the bundle field and run the update. + $this->addBundleField(); + $this->applyEntityUpdates(); + + // Update the entity type schema to revisionable but don't run the updates + // yet. + $this->updateEntityTypeToRevisionable(); + + // Perform a no-op update on the bundle field, which should work because + // both the storage and the storage schema are using the last installed + // entity type definition. + $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + $entity_definition_update_manager->updateFieldStorageDefinition($entity_definition_update_manager->getFieldStorageDefinition('new_bundle_field', 'entity_test_update')); + } + /** * Tests creating and deleting a multi-field index when there are no existing entities. */