diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index ede482b..fd3f6bc 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -85,4 +85,18 @@ function resetConfig($name) { $this->configs[$name]->init(); } } + + function renameConfig($old_name, $new_name) { + // This will likely always be TRUE + if (isset($this->configs[$old_name])) { + $config = $this->configs[$old_name]; + // Save a clone of the config object in the old slot + $this->configs[$old_name] = clone $config; + + // Update the name and re-init the object + // then save it in the slot of the new_name + $config->setName($new_name)->init(); + $this->configs[$new_name] = $config; + } + } } diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php index af37199..fef47d1 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php @@ -288,7 +288,14 @@ public function save(EntityInterface $entity) { } $config = config($prefix . $id); $is_new = $config->isNew(); - $config->setName($prefix . $entity->id()); + + if ($id != $entity->id()) { + // We need to keep three things in mind for a rename: + // * Storage controller needs to access the original object + // * Object should be renamed in ConfigFactory and slot should be updated + // * All instances of the Object should be renamed + drupal_container()->get('config.factory')->renameConfig($prefix . $id, $prefix . $entity->id()); + } if (!$is_new && !isset($entity->original)) { $this->resetCache(array($id)); @@ -383,11 +390,6 @@ protected function postSave(EntityInterface $entity, $update) { if ($update && !empty($entity->original) && $entity->{$this->idKey} !== $entity->original->{$this->idKey}) { // @todo This should just delete the original config object without going // through the API, no? - // @todo: We try to delete the original here, but because ConfigFactory - // still things the new one that has been renamed is the original, we - // end up deleting the new config file instead of the old one. Clearing - // the cache fixes this, but isn't the correct approach. - drupal_container()->get('config.factory')->resetConfigs(); $entity->original->delete(); } }