function CredentialForm::buildForm

Same name in other branches
  1. 8.9.x core/modules/migrate_drupal_ui/src/Form/CredentialForm.php \Drupal\migrate_drupal_ui\Form\CredentialForm::buildForm()
  2. 10 core/modules/migrate_drupal_ui/src/Form/CredentialForm.php \Drupal\migrate_drupal_ui\Form\CredentialForm::buildForm()
  3. 11.x core/modules/migrate_drupal_ui/src/Form/CredentialForm.php \Drupal\migrate_drupal_ui\Form\CredentialForm::buildForm()

Overrides MigrateUpgradeFormBase::buildForm

File

core/modules/migrate_drupal_ui/src/Form/CredentialForm.php, line 83

Class

CredentialForm
Migrate Upgrade database credential form.

Namespace

Drupal\migrate_drupal_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    if ($this->store
        ->get('step') != 'credential') {
        return $this->restartUpgradeForm();
    }
    $form = parent::buildForm($form, $form_state);
    $form['actions']['submit']['#value'] = $this->t('Review upgrade');
    $form['#title'] = $this->t('Drupal Upgrade');
    $drivers = $this->getDatabaseTypes();
    $drivers_keys = array_keys($drivers);
    $default_driver = current($drivers_keys);
    $default_options = [];
    $form['help'] = [
        '#type' => 'item',
        '#description' => $this->t('Provide the information to access the Drupal site you want to upgrade. Files can be imported into the upgraded site as well.  See the <a href=":url">Upgrade documentation for more detailed instructions</a>.', [
            ':url' => 'https://www.drupal.org/upgrade/migrate',
        ]),
    ];
    $migrate_source_version = Settings::get('migrate_source_version') == '6' ? '6' : '7';
    $form['version'] = [
        '#type' => 'radios',
        '#default_value' => $migrate_source_version,
        '#title' => $this->t('Drupal version of the source site'),
        '#options' => [
            '6' => $this->t('Drupal 6'),
            '7' => $this->t('Drupal 7'),
        ],
        '#required' => TRUE,
    ];
    $available_connections = array_diff(array_keys(Database::getAllConnectionInfo()), [
        'default',
    ]);
    $options = array_combine($available_connections, $available_connections);
    $migrate_source_connection = Settings::get('migrate_source_connection');
    $preferred_connections = $migrate_source_connection ? [
        'migrate',
        $migrate_source_connection,
    ] : [
        'migrate',
    ];
    $default_options = array_intersect($preferred_connections, $available_connections);
    $form['source_connection'] = [
        '#type' => 'select',
        '#title' => $this->t('Source connection'),
        '#options' => $options,
        '#default_value' => array_pop($default_options),
        '#empty_option' => $this->t('- User defined -'),
        '#description' => $this->t('Choose one of the keys from the $databases array or else select "User defined" and enter database credentials.'),
        '#access' => !empty($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 upgrade.'),
        '#open' => TRUE,
        '#states' => [
            'visible' => [
                ':input[name=source_connection]' => [
                    'value' => '',
                ],
            ],
        ],
    ];
    $form['database']['driver'] = [
        '#type' => 'radios',
        '#title' => $this->t('Database type'),
        '#required' => TRUE,
        '#default_value' => $default_driver,
        '#states' => [
            'required' => [
                ':input[name=source_connection]' => [
                    'value' => '',
                ],
            ],
        ],
    ];
    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);
        unset($form['database']['settings'][$key]['advanced_options']['prefix']['#description']);
        // This is a multi-step form and is not rebuilt during submission so
        // #limit_validation_errors is not used. The database and username fields
        // for mysql and pgsql must not be required.
        $form['database']['settings'][$key]['database']['#required'] = FALSE;
        $form['database']['settings'][$key]['username']['#required'] = FALSE;
        $form['database']['settings'][$key]['database']['#states'] = [
            'required' => [
                ':input[name=source_connection]' => [
                    'value' => '',
                ],
                ':input[name=driver]' => [
                    'value' => $key,
                ],
            ],
        ];
        if ($key != 'sqlite') {
            $form['database']['settings'][$key]['username']['#states'] = [
                'required' => [
                    ':input[name=source_connection]' => [
                        'value' => '',
                    ],
                    ':input[name=driver]' => [
                        'value' => $key,
                    ],
                ],
            ];
            $form['database']['settings'][$key]['password']['#states'] = [
                'required' => [
                    ':input[name=source_connection]' => [
                        'value' => '',
                    ],
                    ':input[name=driver]' => [
                        'value' => $key,
                    ],
                ],
            ];
        }
        $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['source'] = [
        '#type' => 'details',
        '#title' => $this->t('Source files'),
        '#open' => TRUE,
    ];
    $form['source']['d6_source_base_path'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Document root for files'),
        '#default_value' => Settings::get('migrate_file_public_path') ?? '',
        '#description' => $this->t('To import files from your current Drupal site, enter a local file directory containing your site (e.g. /var/www/docroot), or your site address (for example http://example.com).'),
        '#states' => [
            'visible' => [
                ':input[name="version"]' => [
                    'value' => '6',
                ],
            ],
        ],
        '#element_validate' => [
            '::validatePaths',
        ],
    ];
    $form['source']['source_base_path'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Document root for public files'),
        '#default_value' => Settings::get('migrate_file_public_path') ?? '',
        '#description' => $this->t('To import public files from your current Drupal site, enter a local file directory containing your site (e.g. /var/www/docroot), or your site address (for example http://example.com).'),
        '#states' => [
            'visible' => [
                ':input[name="version"]' => [
                    'value' => '7',
                ],
            ],
        ],
        '#element_validate' => [
            '::validatePaths',
        ],
    ];
    $form['source']['source_private_file_path'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Document root for private files'),
        '#default_value' => Settings::get('migrate_file_private_path') ?? '',
        '#description' => $this->t('To import private files from your current Drupal site, enter a local file directory containing your site (e.g. /var/www/docroot). Leave blank to use the same value as Public files directory.'),
        '#states' => [
            'visible' => [
                ':input[name="version"]' => [
                    'value' => '7',
                ],
            ],
        ],
        '#element_validate' => [
            '::validatePaths',
        ],
    ];
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.