PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/web/core/modules/migrate_drupal_ui/src/Form/ReviewForm.php

https://gitlab.com/andecode/theme-spark
PHP | 323 lines | 199 code | 29 blank | 95 comment | 13 complexity | c58e8fb730da5fd6d3c728869e4b65b1 MD5 | raw file
  1. <?php
  2. namespace Drupal\migrate_drupal_ui\Form;
  3. use Drupal\Core\Batch\BatchBuilder;
  4. use Drupal\Core\Config\ConfigFactoryInterface;
  5. use Drupal\Core\Extension\Exception\UnknownExtensionException;
  6. use Drupal\Core\Extension\ModuleHandlerInterface;
  7. use Drupal\Core\Form\FormStateInterface;
  8. use Drupal\Core\State\StateInterface;
  9. use Drupal\Core\TempStore\PrivateTempStoreFactory;
  10. use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
  11. use Drupal\migrate_drupal\MigrationState;
  12. use Drupal\migrate_drupal_ui\Batch\MigrateUpgradeImportBatch;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. /**
  15. * Migrate Upgrade review form.
  16. *
  17. * This confirmation form provides the user with a summary of all the modules
  18. * enabled on the source site and whether they will be upgraded or not. Data
  19. * from a module's .migrate_drupal.yml file and all the migration plugins
  20. * (source, destination and field) for each enabled Drupal 8 module are used to
  21. * decide the migration status for each enabled module on the source site.
  22. *
  23. * The migration status displayed on the Review page is a list of all the
  24. * enabled modules on the source site divided into two categories, those that
  25. * will not be upgraded and those that will be upgraded. The intention is to
  26. * provide the admin with enough information to decide if it is OK to proceed
  27. * with the upgrade.
  28. *
  29. * @internal
  30. */
  31. class ReviewForm extends MigrateUpgradeFormBase {
  32. /**
  33. * The migrations.
  34. *
  35. * @var \Drupal\migrate\Plugin\MigrationInterface[]
  36. */
  37. protected $migrations;
  38. /**
  39. * Migration state service.
  40. *
  41. * @var \Drupal\migrate_drupal\MigrationState
  42. */
  43. protected $migrationState;
  44. /**
  45. * Module handler.
  46. *
  47. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  48. */
  49. protected $moduleHandler;
  50. /**
  51. * Source system data set in buildForm().
  52. *
  53. * @var array
  54. */
  55. protected $systemData;
  56. /**
  57. * ReviewForm constructor.
  58. *
  59. * @param \Drupal\Core\State\StateInterface $state
  60. * The state service.
  61. * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
  62. * The migration plugin manager service.
  63. * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $tempstore_private
  64. * The private tempstore factory service.
  65. * @param \Drupal\migrate_drupal\MigrationState $migrationState
  66. * Migration state service.
  67. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  68. * The config factory service.
  69. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  70. * The module handler service.
  71. */
  72. public function __construct(StateInterface $state, MigrationPluginManagerInterface $migration_plugin_manager, PrivateTempStoreFactory $tempstore_private, MigrationState $migrationState, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler = NULL) {
  73. parent::__construct($config_factory, $migration_plugin_manager, $state, $tempstore_private);
  74. $this->migrationState = $migrationState;
  75. if (!$module_handler) {
  76. @trigger_error('Calling ' . __METHOD__ . ' without the $module_handler argument is deprecated in drupal:9.1.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/3136769', E_USER_DEPRECATED);
  77. $module_handler = \Drupal::service('module_handler');
  78. }
  79. $this->moduleHandler = $module_handler;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public static function create(ContainerInterface $container) {
  85. return new static(
  86. $container->get('state'),
  87. $container->get('plugin.manager.migration'),
  88. $container->get('tempstore.private'),
  89. $container->get('migrate_drupal.migration_state'),
  90. $container->get('config.factory'),
  91. $container->get('module_handler')
  92. );
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getFormId() {
  98. return 'migrate_drupal_ui_review_form';
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function buildForm(array $form, FormStateInterface $form_state) {
  104. // Get all the data needed for this form.
  105. $version = $this->store->get('version');
  106. $this->migrations = $this->store->get('migrations');
  107. // Fetch the source system data at the first opportunity.
  108. $this->systemData = $this->store->get('system_data');
  109. // If data is missing or this is the wrong step, start over.
  110. if (!$version || !$this->migrations || !$this->systemData ||
  111. ($this->store->get('step') != 'review')) {
  112. return $this->restartUpgradeForm();
  113. }
  114. $form = parent::buildForm($form, $form_state);
  115. $form['#title'] = $this->t('What will be upgraded?');
  116. $migrations = $this->migrationPluginManager->createInstances(array_keys($this->store->get('migrations')));
  117. // Get the upgrade states for the source modules.
  118. $display = $this->migrationState->getUpgradeStates($version, $this->systemData, $migrations);
  119. // Missing migrations.
  120. $missing_module_list = [
  121. '#type' => 'details',
  122. '#open' => TRUE,
  123. '#title' => $this->t('Modules that will not be upgraded'),
  124. '#summary_attributes' => ['id' => ['error']],
  125. '#description' => $this->t("The new site is missing modules corresponding to the old site's modules. Unless they are installed prior to the upgrade, configuration and/or content needed by them will not be available on your new site. <a href=':review'>Read the checklist</a> to help decide what to do.", [':review' => 'https://www.drupal.org/docs/8/upgrade/upgrade-using-web-browser#pre-upgrade-analysis']),
  126. '#weight' => 2,
  127. ];
  128. $missing_module_list['module_list'] = [
  129. '#type' => 'table',
  130. '#header' => [
  131. $this->t('Drupal @version module name', ['@version' => $version]),
  132. $this->t('Drupal @version machine name', ['@version' => $version]),
  133. $this->t('Drupal @version', ['@version' => $this->destinationSiteVersion]),
  134. ],
  135. ];
  136. $missing_count = 0;
  137. if (isset($display[MigrationState::NOT_FINISHED])) {
  138. $output = $this->prepareOutput($display[MigrationState::NOT_FINISHED]);
  139. foreach ($output as $data) {
  140. $missing_count++;
  141. // Get the migration status for each source module, if a module of the
  142. // same name exists on the destination site.
  143. $missing_module_list['module_list']['#rows'][] = [
  144. [
  145. 'data' => $data['source_module_name'],
  146. 'class' => ['upgrade-analysis-report__status-icon', 'upgrade-analysis-report__status-icon--error'],
  147. ],
  148. $data['source_machine_name'],
  149. $data['destination'],
  150. ];
  151. }
  152. }
  153. // Available migrations.
  154. $available_module_list = [
  155. '#type' => 'details',
  156. '#title' => $this->t('Modules that will be upgraded'),
  157. '#summary_attributes' => ['id' => ['checked']],
  158. '#weight' => 4,
  159. ];
  160. $available_module_list['module_list'] = [
  161. '#type' => 'table',
  162. '#header' => [
  163. $this->t('Drupal @version module name', ['@version' => $version]),
  164. $this->t('Drupal @version machine name', ['@version' => $version]),
  165. $this->t('Drupal @version', ['@version' => $this->destinationSiteVersion]),
  166. ],
  167. ];
  168. $available_count = 0;
  169. if (isset($display[MigrationState::FINISHED])) {
  170. $output = $this->prepareOutput($display[MigrationState::FINISHED]);
  171. foreach ($output as $data) {
  172. $available_count++;
  173. $available_module_list['module_list']['#rows'][] = [
  174. [
  175. 'data' => $data['source_module_name'],
  176. 'class' => ['upgrade-analysis-report__status-icon', 'upgrade-analysis-report__status-icon--checked'],
  177. ],
  178. $data['source_machine_name'],
  179. $data['destination'],
  180. ];
  181. }
  182. }
  183. $counters = [];
  184. $general_info = [];
  185. if ($missing_count) {
  186. $counters[] = [
  187. '#theme' => 'status_report_counter',
  188. '#amount' => $missing_count,
  189. '#text' => $this->formatPlural($missing_count, 'Module will not be upgraded', 'Modules will not be upgraded'),
  190. '#severity' => 'error',
  191. '#weight' => 0,
  192. ];
  193. $general_info[] = $missing_module_list;
  194. }
  195. if ($available_count) {
  196. $counters[] = [
  197. '#theme' => 'status_report_counter',
  198. '#amount' => $available_count,
  199. '#text' => $this->formatPlural($available_count, 'Module will be upgraded', 'Modules will be upgraded'),
  200. '#severity' => 'checked',
  201. '#weight' => 1,
  202. ];
  203. $general_info[] = $available_module_list;
  204. }
  205. $form['status_report_page'] = [
  206. '#theme' => 'status_report_page',
  207. '#counters' => $counters,
  208. '#general_info' => $general_info,
  209. ];
  210. $form['#attached']['library'][] = 'migrate_drupal_ui/base';
  211. return $form;
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function submitForm(array &$form, FormStateInterface $form_state) {
  217. $config['source_base_path'] = $this->store->get('source_base_path');
  218. $config['source_private_file_path'] = $this->store->get('source_private_file_path');
  219. $batch_builder = (new BatchBuilder())
  220. ->setTitle($this->t('Running upgrade'))
  221. ->setProgressMessage('')
  222. ->addOperation([
  223. MigrateUpgradeImportBatch::class,
  224. 'run',
  225. ], [array_keys($this->migrations), $config])
  226. ->setFinishCallback([MigrateUpgradeImportBatch::class, 'finished']);
  227. batch_set($batch_builder->toArray());
  228. $form_state->setRedirect('<front>');
  229. $this->store->set('step', 'overview');
  230. $this->state->set('migrate_drupal_ui.performed', REQUEST_TIME);
  231. }
  232. /**
  233. * {@inheritdoc}
  234. */
  235. public function getConfirmText() {
  236. return $this->t('Perform upgrade');
  237. }
  238. /**
  239. * Prepare the migration state data for output.
  240. *
  241. * Each source and destination module_name is changed to the human-readable
  242. * name, the destination modules are put into a CSV format, and everything is
  243. * sorted.
  244. *
  245. * @param string[] $migration_state
  246. * An array where the keys are machine names of modules on
  247. * the source site. Values are lists of machine names of modules on the
  248. * destination site, in CSV format.
  249. *
  250. * @return string[][]
  251. * An indexed array of arrays that contain module data, sorted by the source
  252. * module name. Each sub-array contains the source module name, the source
  253. * module machine name, and the destination module names in a sorted CSV
  254. * format.
  255. */
  256. protected function prepareOutput(array $migration_state) {
  257. $output = [];
  258. foreach ($migration_state as $source_machine_name => $destination_modules) {
  259. $data = NULL;
  260. if (isset($this->systemData['module'][$source_machine_name]['info'])) {
  261. $data = unserialize($this->systemData['module'][$source_machine_name]['info']);
  262. }
  263. $source_module_name = $data['name'] ?? $source_machine_name;
  264. // Get the names of all the destination modules.
  265. $destination_module_names = [];
  266. if (!empty($destination_modules)) {
  267. $destination_modules = explode(', ', $destination_modules);
  268. foreach ($destination_modules as $destination_module) {
  269. if ($destination_module === 'core') {
  270. $destination_module_names[] = 'Core';
  271. }
  272. else {
  273. try {
  274. $destination_module_names[] = $this->moduleHandler->getName($destination_module);
  275. }
  276. catch (UnknownExtensionException $e) {
  277. $destination_module_names[] = $destination_module;
  278. }
  279. }
  280. }
  281. }
  282. sort($destination_module_names);
  283. $output[$source_machine_name] = [
  284. 'source_module_name' => $source_module_name,
  285. 'source_machine_name' => $source_machine_name,
  286. 'destination' => implode(', ', $destination_module_names),
  287. ];
  288. }
  289. usort($output, function ($a, $b) {
  290. return strcmp($a['source_module_name'], $b['source_module_name']);
  291. });
  292. return $output;
  293. }
  294. }