2492143abc29b4716e4c4f5c2008e0a0681bedc6
[yaffs-website] / web / core / modules / migrate_drupal_ui / src / Form / IdConflictForm.php
1 <?php
2
3 namespace Drupal\migrate_drupal_ui\Form;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\TempStore\PrivateTempStoreFactory;
7 use Drupal\migrate\Audit\IdAuditor;
8 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
9 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Migrate Upgrade Id Conflict form.
14  *
15  * @internal
16  */
17 class IdConflictForm extends MigrateUpgradeFormBase {
18
19   /**
20    * The migration plugin manager service.
21    *
22    * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
23    */
24   protected $pluginManager;
25
26   /**
27    * IdConflictForm constructor.
28    *
29    * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
30    *   The migration plugin manager service.
31    * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $tempstore_private
32    *   The private tempstore factory.
33    */
34   public function __construct(MigrationPluginManagerInterface $migration_plugin_manager, PrivateTempStoreFactory $tempstore_private) {
35     parent::__construct($tempstore_private);
36     $this->pluginManager = $migration_plugin_manager;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function create(ContainerInterface $container) {
43     return new static(
44       $container->get('plugin.manager.migration'),
45       $container->get('tempstore.private')
46     );
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function getFormId() {
53     return 'migrate_drupal_ui_idconflict_form';
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function buildForm(array $form, FormStateInterface $form_state) {
60     // Get all the data needed for this form.
61     $migrations = $this->store->get('migrations');
62
63     // If data is missing or this is the wrong step, start over.
64     if (!$migrations || ($this->store->get('step') != 'idconflict')) {
65       return $this->restartUpgradeForm();
66     }
67
68     $migration_ids = array_keys($migrations);
69     // Check if there are conflicts. If none, just skip this form!
70     $migrations = $this->pluginManager->createInstances($migration_ids);
71
72     $translated_content_conflicts = $content_conflicts = [];
73
74     $results = (new IdAuditor())->auditMultiple($migrations);
75
76     /** @var \Drupal\migrate\Audit\AuditResult $result */
77     foreach ($results as $result) {
78       $destination = $result->getMigration()->getDestinationPlugin();
79       if ($destination instanceof EntityContentBase && $destination->isTranslationDestination()) {
80         // Translations are not yet supported by the audit system. For now, we
81         // only warn the user to be cautious when migrating translated content.
82         // I18n support should be added in https://www.drupal.org/node/2905759.
83         $translated_content_conflicts[] = $result;
84       }
85       elseif (!$result->passed()) {
86         $content_conflicts[] = $result;
87       }
88     }
89
90     if ($content_conflicts || $translated_content_conflicts) {
91       $this->messenger()->addWarning($this->t('WARNING: Content may be overwritten on your new site.'));
92
93       $form = parent::buildForm($form, $form_state);
94       $form['#title'] = $this->t('Upgrade analysis report');
95
96       if ($content_conflicts) {
97         $form = $this->conflictsForm($form, $content_conflicts);
98       }
99       if ($translated_content_conflicts) {
100         $form = $this->i18nWarningForm($form, $translated_content_conflicts);
101       }
102       return $form;
103     }
104     else {
105       $this->store->set('step', 'review');
106       return $this->redirect('migrate_drupal_ui.upgrade_review');
107     }
108   }
109
110   /**
111    * Build the markup for conflict warnings.
112    *
113    * @param array $form
114    *   An associative array containing the structure of the form.
115    * @param \Drupal\migrate\Audit\AuditResult[] $conflicts
116    *   The failing audit results.
117    *
118    * @return array
119    *   The form structure.
120    */
121   protected function conflictsForm(array &$form, array $conflicts) {
122     $form['conflicts'] = [
123       '#title' => $this->t('There is conflicting content of these types:'),
124       '#theme' => 'item_list',
125       '#items' => $this->formatConflicts($conflicts),
126     ];
127
128     $form['warning'] = [
129       '#type' => 'markup',
130       '#markup' => '<p>' . $this->t('It looks like you have content on your new site which <strong>may be overwritten</strong> if you continue to run this upgrade. The upgrade should be performed on a clean Drupal 8 installation. For more information see the <a target="_blank" href=":id-conflicts-handbook">upgrade handbook</a>.', [':id-conflicts-handbook' => 'https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#id_conflicts']) . '</p>',
131     ];
132
133     return $form;
134   }
135
136   /**
137    * Formats a set of failing audit results as strings.
138    *
139    * Each string is the label of the destination plugin of the migration that
140    * failed the audit, keyed by the destination plugin ID in order to prevent
141    * duplication.
142    *
143    * @param \Drupal\migrate\Audit\AuditResult[] $conflicts
144    *   The failing audit results.
145    *
146    * @return string[]
147    *   The formatted audit results.
148    */
149   protected function formatConflicts(array $conflicts) {
150     $items = [];
151
152     foreach ($conflicts as $conflict) {
153       $definition = $conflict->getMigration()->getDestinationPlugin()->getPluginDefinition();
154       $id = $definition['id'];
155       $items[$id] = $definition['label'];
156     }
157     sort($items, SORT_STRING);
158
159     return $items;
160   }
161
162   /**
163    * Build the markup for i18n warnings.
164    *
165    * @param array $form
166    *   An associative array containing the structure of the form.
167    * @param \Drupal\migrate\Audit\AuditResult[] $conflicts
168    *   The failing audit results.
169    *
170    * @return array
171    *   The form structure.
172    */
173   protected function i18nWarningForm(array &$form, array $conflicts) {
174     $form['i18n'] = [
175       '#title' => $this->t('There is translated content of these types:'),
176       '#theme' => 'item_list',
177       '#items' => $this->formatConflicts($conflicts),
178     ];
179
180     $form['i18n_warning'] = [
181       '#type' => 'markup',
182       '#markup' => '<p>' . $this->t('It looks like you are migrating translated content from your old site. Possible ID conflicts for translations are not automatically detected in the current version of Drupal. Refer to the <a target="_blank" href=":id-conflicts-handbook">upgrade handbook</a> for instructions on how to avoid ID conflicts with translated content.', [':id-conflicts-handbook' => 'https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#id_conflicts']) . '</p>',
183     ];
184
185     return $form;
186   }
187
188   /**
189    * {@inheritdoc}
190    */
191   public function submitForm(array &$form, FormStateInterface $form_state) {
192     $this->store->set('step', 'review');
193     $form_state->setRedirect('migrate_drupal_ui.upgrade_review');
194   }
195
196   /**
197    * {@inheritdoc}
198    */
199   public function getConfirmText() {
200     return $this->t('I acknowledge I may lose data. Continue anyway.');
201   }
202
203 }