View source
<?php
namespace Drupal\migrate_drupal_ui\Form;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate_drupal\MigrationState;
use Drupal\migrate_drupal_ui\Batch\MigrateUpgradeImportBatch;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ReviewForm extends MigrateUpgradeFormBase {
protected $migrations;
protected $migrationState;
protected $moduleHandler;
protected $systemData;
public function __construct(StateInterface $state, MigrationPluginManagerInterface $migration_plugin_manager, PrivateTempStoreFactory $tempstore_private, MigrationState $migrationState, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory, $migration_plugin_manager, $state, $tempstore_private);
$this->migrationState = $migrationState;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('state'), $container
->get('plugin.manager.migration'), $container
->get('tempstore.private'), $container
->get('migrate_drupal.migration_state'), $container
->get('config.factory'), $container
->get('module_handler'));
}
public function getFormId() {
return 'migrate_drupal_ui_review_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$version = $this->store
->get('version');
$this->migrations = $this->store
->get('migrations');
$this->systemData = $this->store
->get('system_data');
if (!$version || !$this->migrations || !$this->systemData || $this->store
->get('step') != 'review') {
return $this
->restartUpgradeForm();
}
$form = parent::buildForm($form, $form_state);
$form['#title'] = $this
->t('What will be upgraded?');
$migrations = $this->migrationPluginManager
->createInstances(array_keys($this->store
->get('migrations')));
$display = $this->migrationState
->getUpgradeStates($version, $this->systemData, $migrations);
$missing_module_list = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Modules that will not be upgraded'),
'#summary_attributes' => [
'id' => [
'error',
],
],
'#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',
]),
'#weight' => 2,
];
$missing_module_list['module_list'] = [
'#type' => 'table',
'#header' => [
$this
->t('Drupal @version module name', [
'@version' => $version,
]),
$this
->t('Drupal @version machine name', [
'@version' => $version,
]),
$this
->t('Drupal @version', [
'@version' => $this->destinationSiteVersion,
]),
],
];
$missing_count = 0;
if (isset($display[MigrationState::NOT_FINISHED])) {
$output = $this
->prepareOutput($display[MigrationState::NOT_FINISHED]);
foreach ($output as $data) {
$missing_count++;
$missing_module_list['module_list']['#rows'][] = [
[
'data' => $data['source_module_name'],
'class' => [
'upgrade-analysis-report__status-icon',
'upgrade-analysis-report__status-icon--error',
],
],
$data['source_machine_name'],
$data['destination'],
];
}
}
$available_module_list = [
'#type' => 'details',
'#title' => $this
->t('Modules that will be upgraded'),
'#summary_attributes' => [
'id' => [
'checked',
],
],
'#weight' => 4,
];
$available_module_list['module_list'] = [
'#type' => 'table',
'#header' => [
$this
->t('Drupal @version module name', [
'@version' => $version,
]),
$this
->t('Drupal @version machine name', [
'@version' => $version,
]),
$this
->t('Drupal @version', [
'@version' => $this->destinationSiteVersion,
]),
],
];
$available_count = 0;
if (isset($display[MigrationState::FINISHED])) {
$output = $this
->prepareOutput($display[MigrationState::FINISHED]);
foreach ($output as $data) {
$available_count++;
$available_module_list['module_list']['#rows'][] = [
[
'data' => $data['source_module_name'],
'class' => [
'upgrade-analysis-report__status-icon',
'upgrade-analysis-report__status-icon--checked',
],
],
$data['source_machine_name'],
$data['destination'],
];
}
}
$counters = [];
$general_info = [];
if ($missing_count) {
$counters[] = [
'#theme' => 'status_report_counter',
'#amount' => $missing_count,
'#text' => $this
->formatPlural($missing_count, 'Module will not be upgraded', 'Modules will not be upgraded'),
'#severity' => 'error',
'#weight' => 0,
];
$general_info[] = $missing_module_list;
}
if ($available_count) {
$counters[] = [
'#theme' => 'status_report_counter',
'#amount' => $available_count,
'#text' => $this
->formatPlural($available_count, 'Module will be upgraded', 'Modules will be upgraded'),
'#severity' => 'checked',
'#weight' => 1,
];
$general_info[] = $available_module_list;
}
$form['status_report_page'] = [
'#theme' => 'status_report_page',
'#counters' => $counters,
'#general_info' => $general_info,
];
$form['#attached']['library'][] = 'migrate_drupal_ui/base';
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config['source_base_path'] = $this->store
->get('source_base_path');
$config['source_private_file_path'] = $this->store
->get('source_private_file_path');
$batch_builder = (new BatchBuilder())
->setTitle($this
->t('Running upgrade'))
->setProgressMessage('')
->addOperation([
MigrateUpgradeImportBatch::class,
'run',
], [
array_keys($this->migrations),
$config,
])
->setFinishCallback([
MigrateUpgradeImportBatch::class,
'finished',
]);
batch_set($batch_builder
->toArray());
$form_state
->setRedirect('<front>');
$this->store
->set('step', 'overview');
$this->state
->set('migrate_drupal_ui.performed', REQUEST_TIME);
}
public function getConfirmText() {
return $this
->t('Perform upgrade');
}
protected function prepareOutput(array $migration_state) {
$output = [];
foreach ($migration_state as $source_machine_name => $destination_modules) {
$data = NULL;
if (isset($this->systemData['module'][$source_machine_name]['info'])) {
$data = unserialize($this->systemData['module'][$source_machine_name]['info']);
}
$source_module_name = $data['name'] ?? $source_machine_name;
$destination_module_names = [];
if (!empty($destination_modules)) {
$destination_modules = explode(', ', $destination_modules);
foreach ($destination_modules as $destination_module) {
if ($destination_module === 'core') {
$destination_module_names[] = 'Core';
}
else {
try {
$destination_module_names[] = $this->moduleHandler
->getName($destination_module);
} catch (UnknownExtensionException $e) {
$destination_module_names[] = $destination_module;
}
}
}
}
sort($destination_module_names);
$output[$source_machine_name] = [
'source_module_name' => $source_module_name,
'source_machine_name' => $source_machine_name,
'destination' => implode(', ', $destination_module_names),
];
}
usort($output, function ($a, $b) {
return strcmp($a['source_module_name'], $b['source_module_name']);
});
return $output;
}
}