public function SiteSettingsForm::buildForm

public SiteSettingsForm::buildForm(array $form, FormStateInterface $form_state)

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 62

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];
  }
  // If there is no database information yet, suggest the first available driver
  // as default value, so that its settings form is made visible via #states
  // when JavaScript is enabled (see below).
  else {
    $default_driver = current($drivers_keys);
    $default_options = array();
  }

  $form['driver'] = array(
    '#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', array('@driver_name' => $driver->name())) . '</h2>';
    $form['settings'][$key]['#type'] = 'container';
    $form['settings'][$key]['#tree'] = TRUE;
    $form['settings'][$key]['advanced_options']['#parents'] = array($key);
    $form['settings'][$key]['#states'] = array(
      'visible' => array(
        ':input[name=driver]' => array('value' => $key),
      )
    );
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => $this->t('Save and continue'),
    '#button_type' => 'primary',
    '#limit_validation_errors' => array(
      array('driver'),
      array($default_driver),
    ),
    '#submit' => array('::submitForm'),
  );

  $form['errors'] = array();
  $form['settings_file'] = array('#type' => 'value', '#value' => $settings_file);

  return $form;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Installer!Form!SiteSettingsForm.php/function/SiteSettingsForm::buildForm/8.1.x