You are here

public function ContentSync::buildForm in Content Synchronization 8

Same name and namespace in other branches
  1. 8.2 src/Form/ContentSync.php \Drupal\content_sync\Form\ContentSync::buildForm()
  2. 3.0.x src/Form/ContentSync.php \Drupal\content_sync\Form\ContentSync::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/ContentSync.php, line 173

Class

ContentSync
Construct the storage changes in a content synchronization form.

Namespace

Drupal\content_sync\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Import all'),
  ];
  $source_list = $this->syncStorage
    ->listAll();
  $storage_comparer = new StorageComparer($this->syncStorage, $this->activeStorage, $this->configManager);
  if (empty($source_list) || !$storage_comparer
    ->createChangelist()
    ->hasChanges()) {
    $form['no_changes'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Name'),
        $this
          ->t('Operations'),
      ],
      '#rows' => [],
      '#empty' => $this
        ->t('There are no content changes to import.'),
    ];
    $form['actions']['#access'] = FALSE;
    return $form;
  }
  else {

    // Validate site uuid unless bypass the validation is selected
    $config = \Drupal::config('content_sync.settings');
    if ($config
      ->get('content_sync.site_uuid_override') == FALSE) {

      // Get site uuid from site settings configuration.
      $site_config = \Drupal::config('system.site');
      $target = $site_config
        ->get('uuid');

      // Get site uuid from content sync folder
      $source = $this->syncStorage
        ->read('site.uuid');
      if ($source['site_uuid'] !== $target) {
        drupal_set_message($this
          ->t('The staged content cannot be imported, because it originates from a different site than this site. You can only synchronize content between cloned instances of this site.'), 'error');
        $form['actions']['#access'] = FALSE;
        return $form;
      }
    }
  }

  // Store the comparer for use in the submit.
  $form_state
    ->set('storage_comparer', $storage_comparer);

  // Add the AJAX library to the form for dialog support.
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  foreach ($storage_comparer
    ->getAllCollectionNames() as $collection) {
    if ($collection != StorageInterface::DEFAULT_COLLECTION) {
      $form[$collection]['collection_heading'] = [
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => $this
          ->t('@collection configuration collection', [
          '@collection' => $collection,
        ]),
      ];
    }
    foreach ($storage_comparer
      ->getChangelist(NULL, $collection) as $config_change_type => $config_names) {
      if (empty($config_names)) {
        continue;
      }

      // @todo A table caption would be more appropriate, but does not have the
      //   visual importance of a heading.
      $form[$collection][$config_change_type]['heading'] = [
        '#type' => 'html_tag',
        '#tag' => 'h3',
      ];
      switch ($config_change_type) {
        case 'create':
          $form[$collection][$config_change_type]['heading']['#value'] = $this
            ->formatPlural(count($config_names), '@count new', '@count new');
          break;
        case 'update':
          $form[$collection][$config_change_type]['heading']['#value'] = $this
            ->formatPlural(count($config_names), '@count changed', '@count changed');
          break;
        case 'delete':
          $form[$collection][$config_change_type]['heading']['#value'] = $this
            ->formatPlural(count($config_names), '@count removed', '@count removed');
          break;
        case 'rename':
          $form[$collection][$config_change_type]['heading']['#value'] = $this
            ->formatPlural(count($config_names), '@count renamed', '@count renamed');
          break;
      }
      $form[$collection][$config_change_type]['list'] = [
        '#type' => 'table',
        '#header' => [
          $this
            ->t('Name'),
          $this
            ->t('Operations'),
        ],
      ];
      foreach ($config_names as $config_name) {
        if ($config_change_type == 'rename') {
          $names = $storage_comparer
            ->extractRenameNames($config_name);
          $route_options = [
            'source_name' => $names['old_name'],
            'target_name' => $names['new_name'],
          ];
          $config_name = $this
            ->t('@source_name to @target_name', [
            '@source_name' => $names['old_name'],
            '@target_name' => $names['new_name'],
          ]);
        }
        else {
          $route_options = [
            'source_name' => $config_name,
          ];
        }
        if ($collection != StorageInterface::DEFAULT_COLLECTION) {
          $route_name = 'content.diff_collection';
          $route_options['collection'] = $collection;
        }
        else {
          $route_name = 'content.diff';
        }
        $links['view_diff'] = [
          'title' => $this
            ->t('View differences'),
          'url' => Url::fromRoute($route_name, $route_options),
          'attributes' => [
            'class' => [
              'use-ajax',
            ],
            'data-dialog-type' => 'modal',
            'data-dialog-options' => json_encode([
              'width' => 700,
            ]),
          ],
        ];
        $form[$collection][$config_change_type]['list']['#rows'][] = [
          'name' => $config_name,
          'operations' => [
            'data' => [
              '#type' => 'operations',
              '#links' => $links,
            ],
          ],
        ];
      }
    }
  }
  return $form;
}