3 namespace Drupal\migrate_drupal_ui\Form;
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;
13 * Migrate Upgrade Id Conflict form.
17 class IdConflictForm extends MigrateUpgradeFormBase {
20 * The migration plugin manager service.
22 * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
24 protected $pluginManager;
27 * IdConflictForm constructor.
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.
34 public function __construct(MigrationPluginManagerInterface $migration_plugin_manager, PrivateTempStoreFactory $tempstore_private) {
35 parent::__construct($tempstore_private);
36 $this->pluginManager = $migration_plugin_manager;
42 public static function create(ContainerInterface $container) {
44 $container->get('plugin.manager.migration'),
45 $container->get('tempstore.private')
52 public function getFormId() {
53 return 'migrate_drupal_ui_idconflict_form';
59 public function buildForm(array $form, FormStateInterface $form_state) {
60 // Get all the data needed for this form.
61 $migrations = $this->store->get('migrations');
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();
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);
72 $translated_content_conflicts = $content_conflicts = [];
74 $results = (new IdAuditor())->auditMultiple($migrations);
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;
85 elseif (!$result->passed()) {
86 $content_conflicts[] = $result;
90 if ($content_conflicts || $translated_content_conflicts) {
91 $this->messenger()->addWarning($this->t('WARNING: Content may be overwritten on your new site.'));
93 $form = parent::buildForm($form, $form_state);
94 $form['#title'] = $this->t('Upgrade analysis report');
96 if ($content_conflicts) {
97 $form = $this->conflictsForm($form, $content_conflicts);
99 if ($translated_content_conflicts) {
100 $form = $this->i18nWarningForm($form, $translated_content_conflicts);
105 $this->store->set('step', 'review');
106 return $this->redirect('migrate_drupal_ui.upgrade_review');
111 * Build the markup for conflict warnings.
114 * An associative array containing the structure of the form.
115 * @param \Drupal\migrate\Audit\AuditResult[] $conflicts
116 * The failing audit results.
119 * The form structure.
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),
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>',
137 * Formats a set of failing audit results as strings.
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
143 * @param \Drupal\migrate\Audit\AuditResult[] $conflicts
144 * The failing audit results.
147 * The formatted audit results.
149 protected function formatConflicts(array $conflicts) {
152 foreach ($conflicts as $conflict) {
153 $definition = $conflict->getMigration()->getDestinationPlugin()->getPluginDefinition();
154 $id = $definition['id'];
155 $items[$id] = $definition['label'];
157 sort($items, SORT_STRING);
163 * Build the markup for i18n warnings.
166 * An associative array containing the structure of the form.
167 * @param \Drupal\migrate\Audit\AuditResult[] $conflicts
168 * The failing audit results.
171 * The form structure.
173 protected function i18nWarningForm(array &$form, array $conflicts) {
175 '#title' => $this->t('There is translated content of these types:'),
176 '#theme' => 'item_list',
177 '#items' => $this->formatConflicts($conflicts),
180 $form['i18n_warning'] = [
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>',
191 public function submitForm(array &$form, FormStateInterface $form_state) {
192 $this->store->set('step', 'review');
193 $form_state->setRedirect('migrate_drupal_ui.upgrade_review');
199 public function getConfirmText() {
200 return $this->t('I acknowledge I may lose data. Continue anyway.');