diff --git a/core/lib/Drupal/Core/Config/ConfigInstaller.php b/core/lib/Drupal/Core/Config/ConfigInstaller.php index 6fe1afa..842e422 100644 --- a/core/lib/Drupal/Core/Config/ConfigInstaller.php +++ b/core/lib/Drupal/Core/Config/ConfigInstaller.php @@ -216,16 +216,12 @@ protected function createConfiguration($collection, array $config_to_install) { if ($this->getActiveStorage($collection)->exists($name)) { $id = $entity_storage->getIDFromConfigName($name, $entity_storage->getEntityType()->getConfigPrefix()); $entity = $entity_storage->load($id); - foreach ($new_config->get() as $property => $value) { - $entity->set($property, $value); - } - $entity->save(); + $entity = $entity_storage->updateFromStorageRecord($entity, $new_config->get()); } else { - $entity_storage - ->create($new_config->get()) - ->save(); + $entity = $entity_storage->createFromStorageRecord($new_config->get()); } + $entity->save(); } else { $new_config->save(); diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php index 75506d9..0b97b45 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php @@ -300,7 +300,7 @@ public function getQueryServicename() { * {@inheritdoc} */ public function importCreate($name, Config $new_config, Config $old_config) { - $entity = $this->create($new_config->get()); + $entity = $this->createFromStorageRecord($new_config->get()); $entity->setSyncing(TRUE); $entity->save(); return TRUE; @@ -316,16 +316,7 @@ public function importUpdate($name, Config $new_config, Config $old_config) { throw new ConfigImporterException(String::format('Attempt to update non-existing entity "@id".', array('@id' => $id))); } $entity->setSyncing(TRUE); - $entity->original = clone $entity; - - foreach ($old_config->get() as $property => $value) { - $entity->original->set($property, $value); - } - - foreach ($new_config->get() as $property => $value) { - $entity->set($property, $value); - } - + $entity = $this->updateFromStorageRecord($entity, $new_config->get()); $entity->save(); return TRUE; } @@ -345,15 +336,49 @@ public function importDelete($name, Config $new_config, Config $old_config) { * {@inheritdoc} */ public function importRename($old_name, Config $new_config, Config $old_config) { - $id = static::getIDFromConfigName($old_name, $this->entityType->getConfigPrefix()); - $entity = $this->load($id); - $entity->setSyncing(TRUE); - $data = $new_config->get(); - foreach ($data as $key => $value) { - $entity->set($key, $value); + return $this->importUpdate($old_name, $new_config, $old_config); + } + + /** + * @param array $values + * @return ConfigEntityInterface + */ + public function createFromStorageRecord(array $values) { + // Assign a new UUID if there is none yet. + if ($this->uuidKey && $this->uuidService && !isset($values[$this->uuidKey])) { + $values[$this->uuidKey] = $this->uuidService->generate(); } - $entity->save(); - return TRUE; + // No preCreate is run? Ho hum. + // No UUID is created. Ho hum. + $data = $this->mapFromStorageRecords(array($values)); + $entity = current($data); + $entity->original = clone $entity; + $entity->enforceIsNew(); + $entity->postCreate($this); + + // Modules might need to add or change the data initially held by the new + // entity object, for instance to fill-in default values. + $this->invokeHook('create', $entity); + return $entity; + } + + /** + * @param ConfigEntityInterface $entity + * @param array $values + * @return ConfigEntityInterface + */ + public function updateFromStorageRecord(ConfigEntityInterface $entity, array $values) { + $entity->original = clone $entity; + + $data = $this->mapFromStorageRecords(array($values)); + $updated_entity = current($data); + + foreach (array_keys($values) as $property) { + $value = $updated_entity->get($property); + $entity->set($property, $value); + } + + return $entity; } } diff --git a/core/modules/config/src/Form/ConfigSingleImportForm.php b/core/modules/config/src/Form/ConfigSingleImportForm.php index 0793c58..3638731 100644 --- a/core/modules/config/src/Form/ConfigSingleImportForm.php +++ b/core/modules/config/src/Form/ConfigSingleImportForm.php @@ -245,12 +245,16 @@ public function submitForm(array &$form, array &$form_state) { $this->config($this->data['config_name'])->setData($this->data['import'])->save(); drupal_set_message($this->t('The %name configuration was imported.', array('%name' => $this->data['config_name']))); } - // For a config entity, create a new entity and save it. + // For a config entity, create an entity and save it. else { try { - $entity = $this->entityManager - ->getStorage($this->data['config_type']) - ->create($this->data['import']); + $entity_storage = $this->entityManager->getStorage($this->data['config_type']); + if ($this->configExists) { + $entity = $entity_storage->updateFromStorageRecord($this->configExists, $this->data['import']); + } + else { + $entity = $entity_storage->createFromStorageRecord($this->data['import']); + } $entity->save(); drupal_set_message($this->t('The @entity_type %label was imported.', array('@entity_type' => $entity->getEntityTypeId(), '%label' => $entity->label()))); } diff --git a/core/modules/config/src/Tests/ConfigSingleImportExportTest.php b/core/modules/config/src/Tests/ConfigSingleImportExportTest.php index 5609f8f..7221be4 100644 --- a/core/modules/config/src/Tests/ConfigSingleImportExportTest.php +++ b/core/modules/config/src/Tests/ConfigSingleImportExportTest.php @@ -98,6 +98,26 @@ public function testImport() { $this->assertIdentical($entity->id(), 'second'); $this->assertFalse($entity->status()); $this->assertIdentical($entity->uuid(), $second_uuid); + + // Perform an update. + $import = << 'config_test', + 'import' => $import, + ); + $this->drupalPostForm('admin/config/development/configuration/single/import', $edit, t('Import')); + $this->assertRaw(t('Are you sure you want to update the %name @type?', array('%name' => 'second', '@type' => 'test configuration'))); + $this->drupalPostForm(NULL, array(), t('Confirm')); + $entity = $storage->load('second'); + $this->assertRaw(t('The @entity_type %label was imported.', array('@entity_type' => 'config_test', '%label' => $entity->label()))); + $this->assertIdentical($entity->label(), 'Second updated'); } /**