Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\config\Form;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\ConfigImporterException;
|
Chris@0
|
6 use Drupal\Core\Config\ConfigImporter;
|
Chris@0
|
7 use Drupal\Core\Config\TypedConfigManagerInterface;
|
Chris@0
|
8 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
9 use Drupal\Core\Extension\ModuleInstallerInterface;
|
Chris@0
|
10 use Drupal\Core\Extension\ThemeHandlerInterface;
|
Chris@0
|
11 use Drupal\Core\Config\ConfigManagerInterface;
|
Chris@0
|
12 use Drupal\Core\Form\FormBase;
|
Chris@0
|
13 use Drupal\Core\Config\StorageInterface;
|
Chris@0
|
14 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
15 use Drupal\Core\Lock\LockBackendInterface;
|
Chris@0
|
16 use Drupal\Core\Config\StorageComparer;
|
Chris@0
|
17 use Drupal\Core\Render\RendererInterface;
|
Chris@0
|
18 use Drupal\Core\Url;
|
Chris@0
|
19 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
Chris@0
|
20 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Construct the storage changes in a configuration synchronization form.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @internal
|
Chris@0
|
26 */
|
Chris@0
|
27 class ConfigSync extends FormBase {
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The database lock object.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var \Drupal\Core\Lock\LockBackendInterface
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $lock;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * The sync configuration object.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var \Drupal\Core\Config\StorageInterface
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $syncStorage;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * The active configuration object.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var \Drupal\Core\Config\StorageInterface
|
Chris@0
|
47 */
|
Chris@0
|
48 protected $activeStorage;
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * The snapshot configuration object.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @var \Drupal\Core\Config\StorageInterface
|
Chris@0
|
54 */
|
Chris@0
|
55 protected $snapshotStorage;
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Event dispatcher.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
|
Chris@0
|
61 */
|
Chris@0
|
62 protected $eventDispatcher;
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * The configuration manager.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @var \Drupal\Core\Config\ConfigManagerInterface;
|
Chris@0
|
68 */
|
Chris@0
|
69 protected $configManager;
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * The typed config manager.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @var \Drupal\Core\Config\TypedConfigManagerInterface
|
Chris@0
|
75 */
|
Chris@0
|
76 protected $typedConfigManager;
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * The module handler.
|
Chris@0
|
80 *
|
Chris@0
|
81 * @var \Drupal\Core\Extension\ModuleHandlerInterface
|
Chris@0
|
82 */
|
Chris@0
|
83 protected $moduleHandler;
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * The theme handler.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @var \Drupal\Core\Extension\ThemeHandlerInterface
|
Chris@0
|
89 */
|
Chris@0
|
90 protected $themeHandler;
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * The module installer.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @var \Drupal\Core\Extension\ModuleInstallerInterface
|
Chris@0
|
96 */
|
Chris@0
|
97 protected $moduleInstaller;
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * The renderer.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @var \Drupal\Core\Render\RendererInterface
|
Chris@0
|
103 */
|
Chris@0
|
104 protected $renderer;
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * Constructs the object.
|
Chris@0
|
108 *
|
Chris@0
|
109 * @param \Drupal\Core\Config\StorageInterface $sync_storage
|
Chris@0
|
110 * The source storage.
|
Chris@0
|
111 * @param \Drupal\Core\Config\StorageInterface $active_storage
|
Chris@0
|
112 * The target storage.
|
Chris@0
|
113 * @param \Drupal\Core\Config\StorageInterface $snapshot_storage
|
Chris@0
|
114 * The snapshot storage.
|
Chris@0
|
115 * @param \Drupal\Core\Lock\LockBackendInterface $lock
|
Chris@0
|
116 * The lock object.
|
Chris@0
|
117 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
|
Chris@0
|
118 * Event dispatcher.
|
Chris@0
|
119 * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
|
Chris@0
|
120 * Configuration manager.
|
Chris@0
|
121 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
|
Chris@0
|
122 * The typed configuration manager.
|
Chris@0
|
123 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
124 * The module handler.
|
Chris@0
|
125 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
|
Chris@0
|
126 * The module installer.
|
Chris@0
|
127 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
|
Chris@0
|
128 * The theme handler.
|
Chris@0
|
129 * @param \Drupal\Core\Render\RendererInterface $renderer
|
Chris@0
|
130 * The renderer.
|
Chris@0
|
131 */
|
Chris@0
|
132 public function __construct(StorageInterface $sync_storage, StorageInterface $active_storage, StorageInterface $snapshot_storage, LockBackendInterface $lock, EventDispatcherInterface $event_dispatcher, ConfigManagerInterface $config_manager, TypedConfigManagerInterface $typed_config, ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, ThemeHandlerInterface $theme_handler, RendererInterface $renderer) {
|
Chris@0
|
133 $this->syncStorage = $sync_storage;
|
Chris@0
|
134 $this->activeStorage = $active_storage;
|
Chris@0
|
135 $this->snapshotStorage = $snapshot_storage;
|
Chris@0
|
136 $this->lock = $lock;
|
Chris@0
|
137 $this->eventDispatcher = $event_dispatcher;
|
Chris@0
|
138 $this->configManager = $config_manager;
|
Chris@0
|
139 $this->typedConfigManager = $typed_config;
|
Chris@0
|
140 $this->moduleHandler = $module_handler;
|
Chris@0
|
141 $this->moduleInstaller = $module_installer;
|
Chris@0
|
142 $this->themeHandler = $theme_handler;
|
Chris@0
|
143 $this->renderer = $renderer;
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * {@inheritdoc}
|
Chris@0
|
148 */
|
Chris@0
|
149 public static function create(ContainerInterface $container) {
|
Chris@0
|
150 return new static(
|
Chris@0
|
151 $container->get('config.storage.sync'),
|
Chris@0
|
152 $container->get('config.storage'),
|
Chris@0
|
153 $container->get('config.storage.snapshot'),
|
Chris@0
|
154 $container->get('lock.persistent'),
|
Chris@0
|
155 $container->get('event_dispatcher'),
|
Chris@0
|
156 $container->get('config.manager'),
|
Chris@0
|
157 $container->get('config.typed'),
|
Chris@0
|
158 $container->get('module_handler'),
|
Chris@0
|
159 $container->get('module_installer'),
|
Chris@0
|
160 $container->get('theme_handler'),
|
Chris@0
|
161 $container->get('renderer')
|
Chris@0
|
162 );
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 /**
|
Chris@0
|
166 * {@inheritdoc}
|
Chris@0
|
167 */
|
Chris@0
|
168 public function getFormId() {
|
Chris@0
|
169 return 'config_admin_import_form';
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 /**
|
Chris@0
|
173 * {@inheritdoc}
|
Chris@0
|
174 */
|
Chris@0
|
175 public function buildForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
176 $form['actions'] = ['#type' => 'actions'];
|
Chris@0
|
177 $form['actions']['submit'] = [
|
Chris@0
|
178 '#type' => 'submit',
|
Chris@0
|
179 '#value' => $this->t('Import all'),
|
Chris@0
|
180 ];
|
Chris@0
|
181 $source_list = $this->syncStorage->listAll();
|
Chris@0
|
182 $storage_comparer = new StorageComparer($this->syncStorage, $this->activeStorage, $this->configManager);
|
Chris@0
|
183 if (empty($source_list) || !$storage_comparer->createChangelist()->hasChanges()) {
|
Chris@0
|
184 $form['no_changes'] = [
|
Chris@0
|
185 '#type' => 'table',
|
Chris@0
|
186 '#header' => [$this->t('Name'), $this->t('Operations')],
|
Chris@0
|
187 '#rows' => [],
|
Chris@0
|
188 '#empty' => $this->t('There are no configuration changes to import.'),
|
Chris@0
|
189 ];
|
Chris@0
|
190 $form['actions']['#access'] = FALSE;
|
Chris@0
|
191 return $form;
|
Chris@0
|
192 }
|
Chris@0
|
193 elseif (!$storage_comparer->validateSiteUuid()) {
|
Chris@0
|
194 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');
|
Chris@0
|
195 $form['actions']['#access'] = FALSE;
|
Chris@0
|
196 return $form;
|
Chris@0
|
197 }
|
Chris@0
|
198 // A list of changes will be displayed, so check if the user should be
|
Chris@0
|
199 // warned of potential losses to configuration.
|
Chris@0
|
200 if ($this->snapshotStorage->exists('core.extension')) {
|
Chris@0
|
201 $snapshot_comparer = new StorageComparer($this->activeStorage, $this->snapshotStorage, $this->configManager);
|
Chris@0
|
202 if (!$form_state->getUserInput() && $snapshot_comparer->createChangelist()->hasChanges()) {
|
Chris@0
|
203 $change_list = [];
|
Chris@0
|
204 foreach ($snapshot_comparer->getAllCollectionNames() as $collection) {
|
Chris@0
|
205 foreach ($snapshot_comparer->getChangelist(NULL, $collection) as $config_names) {
|
Chris@0
|
206 if (empty($config_names)) {
|
Chris@0
|
207 continue;
|
Chris@0
|
208 }
|
Chris@0
|
209 foreach ($config_names as $config_name) {
|
Chris@0
|
210 $change_list[] = $config_name;
|
Chris@0
|
211 }
|
Chris@0
|
212 }
|
Chris@0
|
213 }
|
Chris@0
|
214 sort($change_list);
|
Chris@0
|
215 $message = [
|
Chris@0
|
216 [
|
Chris@0
|
217 '#markup' => $this->t('The following items in your active configuration have changes since the last import that may be lost on the next import.')
|
Chris@0
|
218 ],
|
Chris@0
|
219 [
|
Chris@0
|
220 '#theme' => 'item_list',
|
Chris@0
|
221 '#items' => $change_list,
|
Chris@0
|
222 ]
|
Chris@0
|
223 ];
|
Chris@0
|
224 drupal_set_message($this->renderer->renderPlain($message), 'warning');
|
Chris@0
|
225 }
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 // Store the comparer for use in the submit.
|
Chris@0
|
229 $form_state->set('storage_comparer', $storage_comparer);
|
Chris@0
|
230
|
Chris@0
|
231 // Add the AJAX library to the form for dialog support.
|
Chris@0
|
232 $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
|
Chris@0
|
233
|
Chris@0
|
234 foreach ($storage_comparer->getAllCollectionNames() as $collection) {
|
Chris@0
|
235 if ($collection != StorageInterface::DEFAULT_COLLECTION) {
|
Chris@0
|
236 $form[$collection]['collection_heading'] = [
|
Chris@0
|
237 '#type' => 'html_tag',
|
Chris@0
|
238 '#tag' => 'h2',
|
Chris@0
|
239 '#value' => $this->t('@collection configuration collection', ['@collection' => $collection]),
|
Chris@0
|
240 ];
|
Chris@0
|
241 }
|
Chris@0
|
242 foreach ($storage_comparer->getChangelist(NULL, $collection) as $config_change_type => $config_names) {
|
Chris@0
|
243 if (empty($config_names)) {
|
Chris@0
|
244 continue;
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 // @todo A table caption would be more appropriate, but does not have the
|
Chris@0
|
248 // visual importance of a heading.
|
Chris@0
|
249 $form[$collection][$config_change_type]['heading'] = [
|
Chris@0
|
250 '#type' => 'html_tag',
|
Chris@0
|
251 '#tag' => 'h3',
|
Chris@0
|
252 ];
|
Chris@0
|
253 switch ($config_change_type) {
|
Chris@0
|
254 case 'create':
|
Chris@0
|
255 $form[$collection][$config_change_type]['heading']['#value'] = $this->formatPlural(count($config_names), '@count new', '@count new');
|
Chris@0
|
256 break;
|
Chris@0
|
257
|
Chris@0
|
258 case 'update':
|
Chris@0
|
259 $form[$collection][$config_change_type]['heading']['#value'] = $this->formatPlural(count($config_names), '@count changed', '@count changed');
|
Chris@0
|
260 break;
|
Chris@0
|
261
|
Chris@0
|
262 case 'delete':
|
Chris@0
|
263 $form[$collection][$config_change_type]['heading']['#value'] = $this->formatPlural(count($config_names), '@count removed', '@count removed');
|
Chris@0
|
264 break;
|
Chris@0
|
265
|
Chris@0
|
266 case 'rename':
|
Chris@0
|
267 $form[$collection][$config_change_type]['heading']['#value'] = $this->formatPlural(count($config_names), '@count renamed', '@count renamed');
|
Chris@0
|
268 break;
|
Chris@0
|
269 }
|
Chris@0
|
270 $form[$collection][$config_change_type]['list'] = [
|
Chris@0
|
271 '#type' => 'table',
|
Chris@0
|
272 '#header' => [$this->t('Name'), $this->t('Operations')],
|
Chris@0
|
273 ];
|
Chris@0
|
274
|
Chris@0
|
275 foreach ($config_names as $config_name) {
|
Chris@0
|
276 if ($config_change_type == 'rename') {
|
Chris@0
|
277 $names = $storage_comparer->extractRenameNames($config_name);
|
Chris@0
|
278 $route_options = ['source_name' => $names['old_name'], 'target_name' => $names['new_name']];
|
Chris@0
|
279 $config_name = $this->t('@source_name to @target_name', ['@source_name' => $names['old_name'], '@target_name' => $names['new_name']]);
|
Chris@0
|
280 }
|
Chris@0
|
281 else {
|
Chris@0
|
282 $route_options = ['source_name' => $config_name];
|
Chris@0
|
283 }
|
Chris@0
|
284 if ($collection != StorageInterface::DEFAULT_COLLECTION) {
|
Chris@0
|
285 $route_name = 'config.diff_collection';
|
Chris@0
|
286 $route_options['collection'] = $collection;
|
Chris@0
|
287 }
|
Chris@0
|
288 else {
|
Chris@0
|
289 $route_name = 'config.diff';
|
Chris@0
|
290 }
|
Chris@0
|
291 $links['view_diff'] = [
|
Chris@0
|
292 'title' => $this->t('View differences'),
|
Chris@0
|
293 'url' => Url::fromRoute($route_name, $route_options),
|
Chris@0
|
294 'attributes' => [
|
Chris@0
|
295 'class' => ['use-ajax'],
|
Chris@0
|
296 'data-dialog-type' => 'modal',
|
Chris@0
|
297 'data-dialog-options' => json_encode([
|
Chris@0
|
298 'width' => 700
|
Chris@0
|
299 ]),
|
Chris@0
|
300 ],
|
Chris@0
|
301 ];
|
Chris@0
|
302 $form[$collection][$config_change_type]['list']['#rows'][] = [
|
Chris@0
|
303 'name' => $config_name,
|
Chris@0
|
304 'operations' => [
|
Chris@0
|
305 'data' => [
|
Chris@0
|
306 '#type' => 'operations',
|
Chris@0
|
307 '#links' => $links,
|
Chris@0
|
308 ],
|
Chris@0
|
309 ],
|
Chris@0
|
310 ];
|
Chris@0
|
311 }
|
Chris@0
|
312 }
|
Chris@0
|
313 }
|
Chris@0
|
314 return $form;
|
Chris@0
|
315 }
|
Chris@0
|
316
|
Chris@0
|
317 /**
|
Chris@0
|
318 * {@inheritdoc}
|
Chris@0
|
319 */
|
Chris@0
|
320 public function submitForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
321 $config_importer = new ConfigImporter(
|
Chris@0
|
322 $form_state->get('storage_comparer'),
|
Chris@0
|
323 $this->eventDispatcher,
|
Chris@0
|
324 $this->configManager,
|
Chris@0
|
325 $this->lock,
|
Chris@0
|
326 $this->typedConfigManager,
|
Chris@0
|
327 $this->moduleHandler,
|
Chris@0
|
328 $this->moduleInstaller,
|
Chris@0
|
329 $this->themeHandler,
|
Chris@0
|
330 $this->getStringTranslation()
|
Chris@0
|
331 );
|
Chris@0
|
332 if ($config_importer->alreadyImporting()) {
|
Chris@0
|
333 drupal_set_message($this->t('Another request may be synchronizing configuration already.'));
|
Chris@0
|
334 }
|
Chris@0
|
335 else {
|
Chris@0
|
336 try {
|
Chris@0
|
337 $sync_steps = $config_importer->initialize();
|
Chris@0
|
338 $batch = [
|
Chris@0
|
339 'operations' => [],
|
Chris@0
|
340 'finished' => [get_class($this), 'finishBatch'],
|
Chris@0
|
341 'title' => t('Synchronizing configuration'),
|
Chris@0
|
342 'init_message' => t('Starting configuration synchronization.'),
|
Chris@0
|
343 'progress_message' => t('Completed step @current of @total.'),
|
Chris@0
|
344 'error_message' => t('Configuration synchronization has encountered an error.'),
|
Chris@0
|
345 ];
|
Chris@0
|
346 foreach ($sync_steps as $sync_step) {
|
Chris@0
|
347 $batch['operations'][] = [[get_class($this), 'processBatch'], [$config_importer, $sync_step]];
|
Chris@0
|
348 }
|
Chris@0
|
349
|
Chris@0
|
350 batch_set($batch);
|
Chris@0
|
351 }
|
Chris@0
|
352 catch (ConfigImporterException $e) {
|
Chris@0
|
353 // There are validation errors.
|
Chris@0
|
354 drupal_set_message($this->t('The configuration cannot be imported because it failed validation for the following reasons:'), 'error');
|
Chris@0
|
355 foreach ($config_importer->getErrors() as $message) {
|
Chris@0
|
356 drupal_set_message($message, 'error');
|
Chris@0
|
357 }
|
Chris@0
|
358 }
|
Chris@0
|
359 }
|
Chris@0
|
360 }
|
Chris@0
|
361
|
Chris@0
|
362 /**
|
Chris@0
|
363 * Processes the config import batch and persists the importer.
|
Chris@0
|
364 *
|
Chris@0
|
365 * @param \Drupal\Core\Config\ConfigImporter $config_importer
|
Chris@0
|
366 * The batch config importer object to persist.
|
Chris@0
|
367 * @param string $sync_step
|
Chris@0
|
368 * The synchronization step to do.
|
Chris@0
|
369 * @param array $context
|
Chris@0
|
370 * The batch context.
|
Chris@0
|
371 */
|
Chris@0
|
372 public static function processBatch(ConfigImporter $config_importer, $sync_step, &$context) {
|
Chris@0
|
373 if (!isset($context['sandbox']['config_importer'])) {
|
Chris@0
|
374 $context['sandbox']['config_importer'] = $config_importer;
|
Chris@0
|
375 }
|
Chris@0
|
376
|
Chris@0
|
377 $config_importer = $context['sandbox']['config_importer'];
|
Chris@0
|
378 $config_importer->doSyncStep($sync_step, $context);
|
Chris@0
|
379 if ($errors = $config_importer->getErrors()) {
|
Chris@0
|
380 if (!isset($context['results']['errors'])) {
|
Chris@0
|
381 $context['results']['errors'] = [];
|
Chris@0
|
382 }
|
Chris@0
|
383 $context['results']['errors'] += $errors;
|
Chris@0
|
384 }
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 /**
|
Chris@0
|
388 * Finish batch.
|
Chris@0
|
389 *
|
Chris@0
|
390 * This function is a static function to avoid serializing the ConfigSync
|
Chris@0
|
391 * object unnecessarily.
|
Chris@0
|
392 */
|
Chris@0
|
393 public static function finishBatch($success, $results, $operations) {
|
Chris@0
|
394 if ($success) {
|
Chris@0
|
395 if (!empty($results['errors'])) {
|
Chris@0
|
396 foreach ($results['errors'] as $error) {
|
Chris@0
|
397 drupal_set_message($error, 'error');
|
Chris@0
|
398 \Drupal::logger('config_sync')->error($error);
|
Chris@0
|
399 }
|
Chris@0
|
400 drupal_set_message(\Drupal::translation()->translate('The configuration was imported with errors.'), 'warning');
|
Chris@0
|
401 }
|
Chris@0
|
402 else {
|
Chris@0
|
403 drupal_set_message(\Drupal::translation()->translate('The configuration was imported successfully.'));
|
Chris@0
|
404 }
|
Chris@0
|
405 }
|
Chris@0
|
406 else {
|
Chris@0
|
407 // An error occurred.
|
Chris@0
|
408 // $operations contains the operations that remained unprocessed.
|
Chris@0
|
409 $error_operation = reset($operations);
|
Chris@0
|
410 $message = \Drupal::translation()->translate('An error occurred while processing %error_operation with arguments: @arguments', ['%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)]);
|
Chris@0
|
411 drupal_set_message($message, 'error');
|
Chris@0
|
412 }
|
Chris@0
|
413 }
|
Chris@0
|
414
|
Chris@0
|
415 }
|