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