ConfigSync::buildForm

public ConfigSync::buildForm(array $form, FormStateInterface $form_state)

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

core/modules/config/src/Form/ConfigSync.php, line 173

Class

ConfigSync
Construct the storage changes in a configuration synchronization form.

Namespace

Drupal\config\Form

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
public function buildForm(array $form, FormStateInterface $form_state) {
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#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'] = array(
      '#type' => 'table',
      '#header' => array($this->t('Name'), $this->t('Operations')),
      '#rows' => array(),
      '#empty' => $this->t('There are no configuration changes to import.'),
    );
    $form['actions']['#access'] = FALSE;
    return $form;
  }
  elseif (!$storage_comparer->validateSiteUuid()) {
    drupal_set_message($this->t('The staged configuration cannot be imported, because it originates from a different site than this site. You can only synchronize configuration between cloned instances of this site.'), 'error');
    $form['actions']['#access'] = FALSE;
    return $form;
  }
  // A list of changes will be displayed, so check if the user should be
  // warned of potential losses to configuration.
  if ($this->snapshotStorage->exists('core.extension')) {
    $snapshot_comparer = new StorageComparer($this->activeStorage, $this->snapshotStorage, $this->configManager);
    if (!$form_state->getUserInput() && $snapshot_comparer->createChangelist()->hasChanges()) {
      $change_list = array();
      foreach ($snapshot_comparer->getAllCollectionNames() as $collection) {
        foreach ($snapshot_comparer->getChangelist(NULL, $collection) as $config_names) {
          if (empty($config_names)) {
            continue;
          }
          foreach ($config_names as $config_name) {
            $change_list[] = $config_name;
          }
        }
      }
      sort($change_list);
      $message = [
        [
          '#markup' => $this->t('The following items in your active configuration have changes since the last import that may be lost on the next import.')
        ],
        [
          '#theme' => 'item_list',
          '#items' => $change_list,
        ]
      ];
      drupal_set_message($this->renderer->renderPlain($message), 'warning');
    }
  }
 
  // 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'] = array(
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => $this->t('@collection configuration collection', array('@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'] = array(
        '#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'] = array(
        '#type' => 'table',
        '#header' => array($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 = array('source_name' => $names['old_name'], 'target_name' => $names['new_name']);
          $config_name = $this->t('@source_name to @target_name', array('@source_name' => $names['old_name'], '@target_name' => $names['new_name']));
        }
        else {
          $route_options = array('source_name' => $config_name);
        }
        if ($collection != StorageInterface::DEFAULT_COLLECTION) {
          $route_name = 'config.diff_collection';
          $route_options['collection'] = $collection;
        }
        else {
          $route_name = 'config.diff';
        }
        $links['view_diff'] = array(
          'title' => $this->t('View differences'),
          'url' => Url::fromRoute($route_name, $route_options),
          'attributes' => array(
            'class' => array('use-ajax'),
            'data-dialog-type' => 'modal',
            'data-dialog-options' => json_encode(array(
              'width' => 700
            )),
          ),
        );
        $form[$collection][$config_change_type]['list']['#rows'][] = array(
          'name' => $config_name,
          'operations' => array(
            'data' => array(
              '#type' => 'operations',
              '#links' => $links,
            ),
          ),
        );
      }
    }
  }
  return $form;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.