ffe899254be5888043927f7d6dca6274b25e85a0
[yaffs-website] / web / core / modules / migrate_drupal_ui / src / Form / MigrateUpgradeFormBase.php
1 <?php
2
3 namespace Drupal\migrate_drupal_ui\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\migrate_drupal\MigrationConfigurationTrait;
8 use Drupal\Core\TempStore\PrivateTempStoreFactory;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Form base for the Migrate Upgrade UI.
13  */
14 abstract class MigrateUpgradeFormBase extends FormBase {
15
16   use MigrationConfigurationTrait;
17
18   /**
19    * Private temporary storage.
20    *
21    * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
22    */
23   protected $store;
24
25   /**
26    * Constructs the Migrate Upgrade Form Base.
27    *
28    * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $tempstore_private
29    *   Private store.
30    */
31   public function __construct(PrivateTempStoreFactory $tempstore_private) {
32     $this->store = $tempstore_private->get('migrate_drupal_ui');
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container) {
39     return new static(
40       $container->get('tempstore.private')
41     );
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function buildForm(array $form, FormStateInterface $form_state) {
48     $form = [];
49     $form['actions']['#type'] = 'actions';
50     $form['actions']['submit'] = [
51       '#type' => 'submit',
52       '#value' => $this->getConfirmText(),
53       '#button_type' => 'primary',
54       '#weight' => 10,
55     ];
56     return $form;
57   }
58
59   /**
60    * Gets and stores information for this migration in temporary store.
61    *
62    * Gets all the migrations, converts each to an array and stores it in the
63    * form state. The source base path for public and private files is also
64    * put into form state.
65    *
66    * @param array $database
67    *   Database array representing the source Drupal database.
68    * @param \Drupal\Core\Form\FormStateInterface $form_state
69    *   The current state of the form.
70    */
71   protected function setupMigrations(array $database, FormStateInterface $form_state) {
72     $connection = $this->getConnection($database);
73     $version = $this->getLegacyDrupalVersion($connection);
74     $this->createDatabaseStateSettings($database, $version);
75     $migrations = $this->getMigrations('migrate_drupal_' . $version, $version);
76
77     // Get the system data from source database.
78     $system_data = $this->getSystemData($connection);
79
80     // Convert the migration object into array
81     // so that it can be stored in form storage.
82     $migration_array = [];
83     foreach ($migrations as $migration) {
84       $migration_array[$migration->id()] = $migration->label();
85     }
86
87     // Store information in the private store.
88     $this->store->set('version', $version);
89     $this->store->set('migrations', $migration_array);
90     if ($version == 6) {
91       $this->store->set('source_base_path', $form_state->getValue('d6_source_base_path'));
92     }
93     else {
94       $this->store->set('source_base_path', $form_state->getValue('source_base_path'));
95     }
96     $this->store->set('source_private_file_path', $form_state->getValue('source_private_file_path'));
97     // Store the retrieved system data in the private store.
98     $this->store->set('system_data', $system_data);
99   }
100
101   /**
102    * Helper to redirect to the Overview form.
103    *
104    * @return \Symfony\Component\HttpFoundation\RedirectResponse
105    *   A redirect response object that may be returned by the controller.
106    */
107   protected function restartUpgradeForm() {
108     $this->store->set('step', 'overview');
109     return $this->redirect('migrate_drupal_ui.upgrade');
110   }
111
112   /**
113    * Returns a caption for the button that confirms the action.
114    *
115    * @return string
116    *   The form confirmation text.
117    */
118   abstract protected function getConfirmText();
119
120 }