function StorageCopyTrait::replaceStorageContents

Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/StorageCopyTrait.php \Drupal\Core\Config\StorageCopyTrait::replaceStorageContents()
  2. 8.9.x core/lib/Drupal/Core/Config/StorageCopyTrait.php \Drupal\Core\Config\StorageCopyTrait::replaceStorageContents()
  3. 11.x core/lib/Drupal/Core/Config/StorageCopyTrait.php \Drupal\Core\Config\StorageCopyTrait::replaceStorageContents()

Copy the configuration from one storage to another and remove stale items.

This method empties target storage and copies all collections from source. Configuration is only copied and not imported, should not be used with the active storage as the target.

Parameters

\Drupal\Core\Config\StorageInterface $source: The configuration storage to copy from.

\Drupal\Core\Config\StorageInterface $target: The configuration storage to copy to.

8 calls to StorageCopyTrait::replaceStorageContents()
CheckpointStorageTest::testWriteOperations in core/tests/Drupal/Tests/Core/Config/Checkpoint/CheckpointStorageTest.php
@covers ::write @covers ::delete @covers ::rename @covers ::deleteAll
ConfigManager::createSnapshot in core/lib/Drupal/Core/Config/ConfigManager.php
ConfigTestTrait::copyConfig in core/tests/Drupal/Tests/ConfigTestTrait.php
Copies configuration objects from source storage to target storage.
ExportStorageManager::getStorage in core/lib/Drupal/Core/Config/ExportStorageManager.php
ImportStorageTransformer::transform in core/lib/Drupal/Core/Config/ImportStorageTransformer.php
Transform the storage to be imported from.

... See full list

File

core/lib/Drupal/Core/Config/StorageCopyTrait.php, line 22

Class

StorageCopyTrait
Utility trait to copy configuration from one storage to another.

Namespace

Drupal\Core\Config

Code

protected static function replaceStorageContents(StorageInterface $source, StorageInterface &$target) {
    // Remove all collections from the target which are not in the source.
    foreach (array_diff($target->getAllCollectionNames(), $source->getAllCollectionNames()) as $collection) {
        // We do this first so we don't have to loop over the added collections.
        $target->createCollection($collection)
            ->deleteAll();
    }
    // Copy all the configuration from all the collections.
    foreach (array_merge([
        StorageInterface::DEFAULT_COLLECTION,
    ], $source->getAllCollectionNames()) as $collection) {
        $source_collection = $source->createCollection($collection);
        $target_collection = $target->createCollection($collection);
        $names = $source_collection->listAll();
        // First we delete all the config which shouldn't be in the target.
        foreach (array_diff($target_collection->listAll(), $names) as $name) {
            $target_collection->delete($name);
        }
        // Then we loop over the config which needs to be there.
        foreach ($names as $name) {
            $data = $source_collection->read($name);
            if ($data !== FALSE) {
                if ($target_collection->read($name) !== $data) {
                    // Update the target collection if the data is different.
                    $target_collection->write($name, $data);
                }
            }
            else {
                $target_collection->delete($name);
                \Drupal::logger('config')->notice('Missing required data for configuration: %config', [
                    '%config' => $name,
                ]);
            }
        }
    }
    // Make sure that the target is set to the same collection as the source.
    $target = $target->createCollection($source->getCollectionName());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.