You are here

public function SiteSettingsForm::buildForm in Drupal 8

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

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

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

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) {
  $settings_file = './' . $this->sitePath . '/settings.php';
  $form['#title'] = $this
    ->t('Database configuration');
  $drivers = drupal_get_database_types();
  $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.
  // 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']['driver'];
    $input[$database['default']['driver']] = $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
      ->name();
    $form['settings'][$key] = $driver
      ->getFormOptions($default_options);
    $form['settings'][$key]['#prefix'] = '<h2 class="js-hide">' . $this
      ->t('@driver_name settings', [
      '@driver_name' => $driver
        ->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;
}