You are here

public function SourceSelectForm::buildForm in Drupal-to-Drupal data migration 8.3

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

migrate_d2d_ui/src/Form/SourceSelectForm.php, line 53

Class

SourceSelectForm
Defines a multi-step form for performing direct site upgrades.

Namespace

Drupal\migrate_d2d_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $drivers = $this
    ->getDatabaseTypes();
  $drivers_keys = array_keys($drivers);

  // @todo https://www.drupal.org/node/2678510 Because this is a multi-step
  //   form, the form is not rebuilt during submission. Ideally we would get
  //   the chosen driver from form input, if available, in order to use
  //   #limit_validation_errors in the same way
  //   \Drupal\Core\Installer\Form\SiteSettingsForm does.
  $default_driver = current($drivers_keys);
  $default_options = [];
  $form['database'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Source database'),
    '#description' => $this
      ->t('Provide credentials for the database of the Drupal site you want to import.'),
    '#open' => TRUE,
  ];
  $form['database']['driver'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Database type'),
    '#required' => TRUE,
    '#default_value' => $default_driver,
  ];
  if (count($drivers) == 1) {
    $form['database']['driver']['#disabled'] = TRUE;
  }

  // Add driver-specific configuration options.
  foreach ($drivers as $key => $driver) {
    $form['database']['driver']['#options'][$key] = $driver
      ->name();
    $form['database']['settings'][$key] = $driver
      ->getFormOptions($default_options);

    // @todo https://www.drupal.org/node/2678510 Using
    //   #limit_validation_errors in the submit does not work so it is not
    //   possible to require the database and username for mysql and pgsql.
    //   This is because this is a multi-step form.
    $form['database']['settings'][$key]['database']['#required'] = FALSE;
    $form['database']['settings'][$key]['username']['#required'] = FALSE;
    $form['database']['settings'][$key]['#prefix'] = '<h2 class="js-hide">' . $this
      ->t('@driver_name settings', [
      '@driver_name' => $driver
        ->name(),
    ]) . '</h2>';
    $form['database']['settings'][$key]['#type'] = 'container';
    $form['database']['settings'][$key]['#tree'] = TRUE;
    $form['database']['settings'][$key]['advanced_options']['#parents'] = [
      $key,
    ];
    $form['database']['settings'][$key]['#states'] = [
      'visible' => [
        ':input[name=driver]' => [
          'value' => $key,
        ],
      ],
    ];

    // Move the host fields out of advanced settings.
    if (isset($form['database']['settings'][$key]['advanced_options']['host'])) {
      $form['database']['settings'][$key]['host'] = $form['database']['settings'][$key]['advanced_options']['host'];
      $form['database']['settings'][$key]['host']['#title'] = 'Database host';
      $form['database']['settings'][$key]['host']['#weight'] = -1;
      unset($form['database']['settings'][$key]['database']['#default_value']);
      unset($form['database']['settings'][$key]['advanced_options']['host']);
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['save'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Review upgrade'),
  ];
  return $form;
}