Skip to content
Snippets Groups Projects
Verified Commit 7d2d9cca authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3038707 by amateescu: The entity type definition is out of sync between...

Issue #3038707 by amateescu: The entity type definition is out of sync between SqlContentEntityStorage and SqlContentEntityStorageSchema during field storage CRUD operations
parent 2dfe7b6d
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityStorageInterface; use Drupal\Core\Entity\FieldableEntityStorageInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
...@@ -81,7 +82,16 @@ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $ ...@@ -81,7 +82,16 @@ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $
// @todo Forward this to all interested handlers, not only storage, once // @todo Forward this to all interested handlers, not only storage, once
// iterating handlers is possible: https://www.drupal.org/node/2332857. // 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) { if ($storage instanceof FieldStorageDefinitionListenerInterface) {
$storage->onFieldStorageDefinitionCreate($storage_definition); $storage->onFieldStorageDefinitionCreate($storage_definition);
...@@ -101,7 +111,16 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $ ...@@ -101,7 +111,16 @@ public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $
// @todo Forward this to all interested handlers, not only storage, once // @todo Forward this to all interested handlers, not only storage, once
// iterating handlers is possible: https://www.drupal.org/node/2332857. // 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) { if ($storage instanceof FieldStorageDefinitionListenerInterface) {
$storage->onFieldStorageDefinitionUpdate($storage_definition, $original); $storage->onFieldStorageDefinitionUpdate($storage_definition, $original);
...@@ -121,7 +140,16 @@ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $ ...@@ -121,7 +140,16 @@ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $
// @todo Forward this to all interested handlers, not only storage, once // @todo Forward this to all interested handlers, not only storage, once
// iterating handlers is possible: https://www.drupal.org/node/2332857. // 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 // 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. // it later during field_purge_batch(), but only if the field has data.
......
...@@ -676,6 +676,25 @@ public function testBundleFieldUpdateWithExistingData() { ...@@ -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. * Tests creating and deleting a multi-field index when there are no existing entities.
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment