function SiteSettingsForm::buildForm

Same name in other branches
  1. 9 core/lib/Drupal/Core/Installer/Form/SiteSettingsForm.php \Drupal\Core\Installer\Form\SiteSettingsForm::buildForm()
  2. 8.9.x core/lib/Drupal/Core/Installer/Form/SiteSettingsForm.php \Drupal\Core\Installer\Form\SiteSettingsForm::buildForm()
  3. 10 core/lib/Drupal/Core/Installer/Form/SiteSettingsForm.php \Drupal\Core\Installer\Form\SiteSettingsForm::buildForm()

Overrides FormInterface::buildForm

File

core/lib/Drupal/Core/Installer/Form/SiteSettingsForm.php, line 61

Class

SiteSettingsForm
Provides a form to configure and rewrite settings.php.

Namespace

Drupal\Core\Installer\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    // Make sure the install API is available.
    include_once DRUPAL_ROOT . '/core/includes/install.inc';
    $settings_file = './' . $this->sitePath . '/settings.php';
    $form['#title'] = $this->t('Database configuration');
    $drivers = $this->databaseDriverList
        ->getInstallableList();
    $drivers_keys = array_keys($drivers);
    // Unless there is input for this form (for a non-interactive installation,
    // input originates from the $settings array passed into install_drupal()),
    // check whether database connection settings have been prepared in
    // settings.php already.
    // Since there could potentially be multiple drivers with the same name,
    // provided by different modules, we fill the 'driver' form field with the
    // driver's namespace, not with the driver name, so to ensure uniqueness of
    // the selection.
    // Note: The installer even executes this form if there is a valid database
    // connection already, since the submit handler of this form is responsible
    // for writing all $settings to settings.php (not limited to $databases).
    $input =& $form_state->getUserInput();
    if (!isset($input['driver']) && ($database = Database::getConnectionInfo())) {
        $input['driver'] = $database['default']['namespace'];
        $input[$database['default']['namespace']] = $database['default'];
    }
    if (isset($input['driver'])) {
        $default_driver = $input['driver'];
        // In case of database connection info from settings.php, as well as for a
        // programmed form submission (non-interactive installer), the table prefix
        // information is usually normalized into an array already, but the form
        // element only allows to configure one default prefix for all tables.
        $prefix =& $input[$default_driver]['prefix'];
        if (isset($prefix) && is_array($prefix)) {
            $prefix = $prefix['default'];
        }
        $default_options = $input[$default_driver];
    }
    else {
        $default_driver = current($drivers_keys);
        $default_options = [];
    }
    $form['driver'] = [
        '#type' => 'radios',
        '#title' => $this->t('Database type'),
        '#required' => TRUE,
        '#default_value' => $default_driver,
    ];
    if (count($drivers) == 1) {
        $form['driver']['#disabled'] = TRUE;
    }
    // Add driver specific configuration options.
    foreach ($drivers as $key => $driver) {
        $form['driver']['#options'][$key] = $driver->getInstallTasks()
            ->name();
        $form['settings'][$key] = $driver->getInstallTasks()
            ->getFormOptions($default_options);
        $form['settings'][$key]['#prefix'] = '<h2 class="js-hide">' . $this->t('@driver_name settings', [
            '@driver_name' => $driver->getInstallTasks()
                ->name(),
        ]) . '</h2>';
        $form['settings'][$key]['#type'] = 'container';
        $form['settings'][$key]['#tree'] = TRUE;
        $form['settings'][$key]['advanced_options']['#parents'] = [
            $key,
        ];
        $form['settings'][$key]['#states'] = [
            'visible' => [
                ':input[name=driver]' => [
                    'value' => $key,
                ],
            ],
        ];
    }
    $form['actions'] = [
        '#type' => 'actions',
    ];
    $form['actions']['save'] = [
        '#type' => 'submit',
        '#value' => $this->t('Save and continue'),
        '#button_type' => 'primary',
        '#limit_validation_errors' => [
            [
                'driver',
            ],
            [
                $default_driver,
            ],
        ],
        '#submit' => [
            '::submitForm',
        ],
    ];
    $form['errors'] = [];
    $form['settings_file'] = [
        '#type' => 'value',
        '#value' => $settings_file,
    ];
    return $form;
}

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