You are here

function config_sync_update_8002 in Configuration Synchronizer 8.2

Fill in missing snapshots for optional configuration.

File

./config_sync.install, line 123
Install, update and uninstall functions for the config_sync module.

Code

function config_sync_update_8002() {
  $active_storage = \Drupal::service('config.storage');
  $provider_storage = \Drupal::service('config_provider.storage');
  $config_lister = \Drupal::service('config_update.config_list');
  $config_differ = \Drupal::service('config_update.config_diff');
  $config_reverter = \Drupal::service('config_update.config_update');
  $extension_changelists = \Drupal::service('config_sync.lister')
    ->getExtensionChangelists();

  // Populate the provider storage with all available configuration.
  \Drupal::service('config_provider.collector')
    ->addInstallableConfig();
  foreach ($extension_changelists as $type => $extensions) {
    foreach ($extensions as $name => $collection_changelists) {

      // Can't use Drupal\config_snapshot\ConfigSnapshotStorageTrait because
      // we're in a static method.
      $service_id = "config_snapshot.{ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET}.{$type}.{$name}";
      if (\Drupal::getContainer() && \Drupal::hasService($service_id)) {
        $snapshot_storage = \Drupal::service($service_id);
      }
      else {
        $snapshot_storage = new ConfigSnapshotStorage(ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET, $type, $name);
      }
      foreach ($collection_changelists as $collection => $operation_types) {

        // Switch collection storages if necessary.
        if ($collection !== $snapshot_storage
          ->getCollectionName()) {
          $snapshot_storage = $snapshot_storage
            ->createCollection($collection);
        }

        // Create operations indicate the configuration is provided but
        // hasn't been snapshotted.
        if (isset($operation_types['create'])) {
          foreach (array_keys($operation_types['create']) as $config_id) {

            // If the item exists but there's no snapshot, create one.
            if ($active_storage
              ->exists($config_id)) {
              $provided_value = $provider_storage
                ->read($config_id);
              $active_value = $active_storage
                ->read($config_id);

              // Snapshot the provided value.
              $snapshot_storage
                ->write($config_id, $provided_value);

              // If the currently active value is not as provided, assume that
              // we have an update and revert to it.
              if (!$config_differ
                ->same($provided_value, $active_value)) {

                // The revert command needs the type and the unprefixed name.
                $config_type = $config_lister
                  ->getTypeNameByConfigName($name);

                // The lister gives NULL if simple configuration, but the
                // reverter expects 'system.simple' so we convert it.
                if ($config_type === NULL) {
                  $config_type = 'system.simple';
                }
                $config_reverter
                  ->revert($config_type, $config_id);
              }
            }
          }
        }
      }
    }
  }
}