You are here

protected function ConfigIgnoreEventSubscriber::transformStorage in Config Ignore 8.3

Makes the import or export storages aware about ignored configs.

Parameters

\Drupal\Core\Config\StorageInterface $transformation_storage: The import or the export storage.

\Drupal\Core\Config\StorageInterface $destination_storage: The active storage on import. The sync storage on export.

2 calls to ConfigIgnoreEventSubscriber::transformStorage()
ConfigIgnoreEventSubscriber::onExportTransform in src/EventSubscriber/ConfigIgnoreEventSubscriber.php
Acts when the storage is transformed for export.
ConfigIgnoreEventSubscriber::onImportTransform in src/EventSubscriber/ConfigIgnoreEventSubscriber.php
Acts when the storage is transformed for import.

File

src/EventSubscriber/ConfigIgnoreEventSubscriber.php, line 130

Class

ConfigIgnoreEventSubscriber
Makes the import/export aware of ignored configs.

Namespace

Drupal\config_ignore\EventSubscriber

Code

protected function transformStorage(StorageInterface $transformation_storage, StorageInterface $destination_storage) {
  $collection_names = array_unique(array_merge($transformation_storage
    ->getAllCollectionNames(), $destination_storage
    ->getAllCollectionNames()));
  array_unshift($collection_names, StorageInterface::DEFAULT_COLLECTION);
  foreach ($collection_names as $collection_name) {
    $destination_storage = $destination_storage
      ->createCollection($collection_name);
    $transformation_storage = $transformation_storage
      ->createCollection($collection_name);

    // Loop over the ignored config in the destination.
    // We need to do this inside of the collection loop because some config
    // to be ignored may only be present in some collections.
    $destination_ignored = $this
      ->getIgnoredConfigs($destination_storage);
    foreach ($destination_ignored as $config_name => $keys) {

      // We just calculated the ignored config based on the storage.
      assert($destination_storage
        ->exists($config_name), "The configuration {$config_name} exists");
      $destination_data = $destination_storage
        ->read($config_name);
      if ($keys === NULL) {

        // The entire config is ignored.
        $transformation_storage
          ->write($config_name, $destination_data);
      }
      else {

        // Only some keys are ignored.
        $source_data = $transformation_storage
          ->read($config_name);
        if ($source_data === FALSE) {

          // The config doesn't exist in the transformation storage but only
          // a key is ignored, we skip writing anything to the transformation
          // storage. But this could be made configurable.
          continue;
        }
        foreach ($keys as $key) {
          if (NestedArray::keyExists($destination_data, $key)) {
            $value = NestedArray::getValue($destination_data, $key);
            NestedArray::setValue($source_data, $key, $value);
          }
          else {
            NestedArray::unsetValue($source_data, $key);
          }
        }
        $transformation_storage
          ->write($config_name, $source_data);
      }
    }

    // Now we get the config to be ignored which exists only in the
    // transformation storage. When importing this means that it is new and
    // when exporting it means it does not exist in the sync directory.
    $transformation_only_ignored = array_diff_key($this
      ->getIgnoredConfigs($transformation_storage), $destination_ignored);
    foreach ($transformation_only_ignored as $config_name => $keys) {
      if ($keys === NULL) {

        // The entire config is ignored.
        $transformation_storage
          ->delete($config_name);
      }
      else {

        // Only some keys are ignored.
        $source_data = $transformation_storage
          ->read($config_name);
        foreach ($keys as $key) {
          NestedArray::unsetValue($source_data, $key);
        }
        $transformation_storage
          ->write($config_name, $source_data);
      }
    }
  }
}