You are here

public function ConfigSplitDiffController::diff in Configuration Split 2.0.x

Shows diff of specified configuration file.

Parameters

string $config_split: The split id.

string $operation: The operation: deactivate, import, export.

string $source_name: The name of the configuration file.

string $target_name: (optional) The name of the target configuration file if different from the $source_name.

string $collection: (optional) The configuration collection name. Defaults to the default collection.

Return value

array|\Symfony\Component\HttpFoundation\Response Table showing a two-way diff between the active and staged configuration.

1 string reference to 'ConfigSplitDiffController::diff'
config_split.routing.yml in ./config_split.routing.yml
config_split.routing.yml

File

src/Controller/ConfigSplitDiffController.php, line 118

Class

ConfigSplitDiffController
The controller to view diffs.

Namespace

Drupal\config_split\Controller

Code

public function diff($config_split, $operation, $source_name, $target_name = NULL, $collection = NULL) {
  $split = $this->configSplitManager
    ->getSplitConfig($config_split);
  if ($split === NULL) {
    return $this
      ->redirect('system.404');
  }
  $headers = [
    'existing' => $this
      ->t('Active'),
    'new' => $this
      ->t('Staged'),
  ];

  // The names for $source_storage and $target_storage are very confusing.
  // But we go with the convention of the diff formatter for now.
  switch ($operation) {
    case 'deactivate':
      $source_storage = $this->activeStorage;
      $target_storage = $this->configSplitManager
        ->singleDeactivate($split, FALSE);
      break;
    case 'activate':
      $source_storage = $this->activeStorage;
      $target_storage = $this->configSplitManager
        ->singleActivate($split, !$split
        ->get('status'));
      break;
    case 'import':
      $source_storage = $this->activeStorage;
      $target_storage = $this->configSplitManager
        ->singleImport($split, !$split
        ->get('status'));
      break;
    case 'export':
      $source_storage = $this->configSplitManager
        ->singleExportTarget($split);
      $target_storage = $this->configSplitManager
        ->singleExportPreview($split);
      $headers = [
        'existing' => $this
          ->t('Existing'),
        'new' => $this
          ->t('New'),
      ];
      break;
    default:
      return $this
        ->redirect('system.404');
  }
  if (!isset($collection)) {
    $collection = StorageInterface::DEFAULT_COLLECTION;
  }
  $diff = $this->configManager
    ->diff($source_storage, $target_storage, $source_name, $target_name, $collection);
  $this->diffFormatter->show_header = FALSE;
  $build = [];
  $build['#title'] = $this
    ->t('View changes of @config_file', [
    '@config_file' => $source_name,
  ]);

  // Add the CSS for the inline diff.
  $build['#attached']['library'][] = 'system/diff';
  $build['diff'] = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'diff',
      ],
    ],
    '#header' => [
      [
        'data' => $headers['existing'],
        'colspan' => '2',
      ],
      [
        'data' => $headers['new'],
        'colspan' => '2',
      ],
    ],
    '#rows' => $this->diffFormatter
      ->format($diff),
  ];
  $build['back'] = [
    '#type' => 'link',
    '#attributes' => [
      'class' => [
        'dialog-cancel',
      ],
    ],
    '#title' => $this
      ->t("Back to overview."),
    '#url' => Url::fromRoute('config.sync'),
  ];
  return $build;
}