diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index 6278472..4affe30 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -402,7 +402,34 @@ public function onEntityTypeDelete(EntityTypeInterface $entity_type) { * {@inheritdoc} */ public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) { + // 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. + // @todo This should be fixed by https://www.drupal.org/node/2274017. + $actual_entity_type_definition = $this->entityType; + if ($last_installed_entity_type_definition = $this->entityManager->getLastInstalledDefinition($this->entityType->id())) { + $this->storage->setEntityType($last_installed_entity_type_definition); + } + + // If the new field storage definition uses setInitialValueFromField(), + // SqlContentEntityStorageSchema will validate the storage definition of + // the "from" field. It must use the last installed version of that, as + // the new field might be created in an update function and the storage + // definition of the "from" field might get changed later. + // @todo This is no longer necessary when + // https://www.drupal.org/node/2554235 is fixed. + $actual_field_storage_definitions = $this->fieldStorageDefinitions; + if ($last_installed_field_storage_definitions = $this->entityManager->getLastInstalledFieldStorageDefinitions($this->entityType->id())) { + $this->fieldStorageDefinitions = $last_installed_field_storage_definitions; + $this->fieldStorageDefinitions[$storage_definition->getName()] = $storage_definition; + } + $this->performFieldSchemaOperation('create', $storage_definition); + + // Restore the previous field storage and entity type definitions. + $this->storage->setEntityType($actual_entity_type_definition); + $this->fieldStorageDefinitions = $actual_field_storage_definitions; } /** diff --git a/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php b/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php index 2961def..bb17fe6 100644 --- a/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php +++ b/core/lib/Drupal/Core/Field/FieldStorageDefinitionListener.php @@ -5,7 +5,6 @@ use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Entity\Sql\SqlContentEntityStorage; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -71,16 +70,6 @@ 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); - - // 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); } @@ -100,16 +89,6 @@ 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); - - // 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); } @@ -129,16 +108,6 @@ 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); - - // 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->onFieldStorageDefinitionDelete($storage_definition); }