Commit 16f07ea5 authored by webchick's avatar webchick

Issue #2245249 by tim.plunkett: Convert installer forms to FormInterface.

parent 43ff331e
This diff is collapsed.
<?php
/**
* @file
* Contains \Drupal\Core\Installer\Form\SelectLanguageForm.
*/
namespace Drupal\Core\Installer\Form;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\UserAgent;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Language\LanguageManager;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides the language selection form.
*/
class SelectLanguageForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'install_select_language_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $install_state = NULL) {
if (count($install_state['translations']) > 1) {
$files = $install_state['translations'];
}
else {
$files = array();
}
$standard_languages = LanguageManager::getStandardLanguageList();
$select_options = array();
$browser_options = array();
$form['#title'] = $this->t('Choose language');
// Build a select list with language names in native language for the user
// to choose from. And build a list of available languages for the browser
// to select the language default from.
if (count($files)) {
// Select lists based on available language files.
foreach ($files as $langcode => $uri) {
$select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode;
$browser_options[] = $langcode;
}
}
else {
// Select lists based on all standard languages.
foreach ($standard_languages as $langcode => $language_names) {
$select_options[$langcode] = $language_names[1];
$browser_options[] = $langcode;
}
}
$request = Request::createFromGlobals();
$browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options);
$form['langcode'] = array(
'#type' => 'select',
'#title' => $this->t('Choose language'),
'#title_display' => 'invisible',
'#options' => $select_options,
// Use the browser detected language as default or English if nothing found.
'#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en',
);
if (empty($files)) {
$form['help'] = array(
'#type' => 'item',
'#markup' => String::format('<p>Translations will be downloaded from the <a href="http://localize.drupal.org">Drupal Translation website</a>.
If you do not want this, select <a href="!english">English</a>.</p>', array(
'!english' => install_full_redirect_url(array('parameters' => array('langcode' => 'en'))),
)),
'#states' => array(
'invisible' => array(
'select[name="langcode"]' => array('value' => 'en'),
),
),
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save and continue'),
'#button_type' => 'primary',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$install_state = &$form_state['build_info']['args'][0];
$install_state['parameters']['langcode'] = $form_state['values']['langcode'];
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Installer\Form\SelectProfileForm.
*/
namespace Drupal\Core\Installer\Form;
use Drupal\Core\Form\FormBase;
/**
* Provides the profile selection form.
*/
class SelectProfileForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'install_select_profile_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $install_state = NULL) {
$form['#title'] = $this->t('Select an installation profile');
$profiles = array();
$names = array();
foreach ($install_state['profiles'] as $profile) {
/** @var $profile \Drupal\Core\Extension\Extension */
$details = install_profile_info($profile->getName());
// Skip this extension if its type is not profile.
if (!isset($details['type']) || $details['type'] != 'profile') {
continue;
}
// Don't show hidden profiles. This is used by to hide the testing profile,
// which only exists to speed up test runs.
if ($details['hidden'] === TRUE) {
continue;
}
$profiles[$profile->getName()] = $details;
// Determine the name of the profile; default to file name if defined name
// is unspecified.
$name = isset($details['name']) ? $details['name'] : $profile->getName();
$names[$profile->getName()] = $name;
}
// Display radio buttons alphabetically by human-readable name, but always
// put the core profiles first (if they are present in the filesystem).
natcasesort($names);
if (isset($names['minimal'])) {
// If the expert ("Minimal") core profile is present, put it in front of
// any non-core profiles rather than including it with them alphabetically,
// since the other profiles might be intended to group together in a
// particular way.
$names = array('minimal' => $names['minimal']) + $names;
}
if (isset($names['standard'])) {
// If the default ("Standard") core profile is present, put it at the very
// top of the list. This profile will have its radio button pre-selected,
// so we want it to always appear at the top.
$names = array('standard' => $names['standard']) + $names;
}
// The profile name and description are extracted for translation from the
// .info file, so we can use $this->t() on them even though they are dynamic
// data at this point.
$form['profile'] = array(
'#type' => 'radios',
'#title' => $this->t('Select an installation profile'),
'#title_display' => 'invisible',
'#options' => array_map(array($this, 't'), $names),
'#default_value' => 'standard',
);
foreach (array_keys($names) as $profile_name) {
$form['profile'][$profile_name]['#description'] = isset($profiles[$profile_name]['description']) ? $this->t($profiles[$profile_name]['description']) : '';
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save and continue'),
'#button_type' => 'primary',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
global $install_state;
$install_state['parameters']['profile'] = $form_state['values']['profile'];
}
}
This diff is collapsed.
<?php
/**
* @file
* Contains \Drupal\Core\Installer\Form\SiteSettingsForm.
*/
namespace Drupal\Core\Installer\Form;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormBase;
/**
* Provides a form to configure and rewrite settings.php.
*/
class SiteSettingsForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'install_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
global $databases;
$conf_path = './' . conf_path(FALSE);
$settings_file = $conf_path . '/settings.php';
$form['#title'] = $this->t('Database configuration');
$drivers = drupal_get_database_types();
$drivers_keys = array_keys($drivers);
// If database connection settings have been prepared in settings.php already,
// then the existing values need to be taken over.
// 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).
if (isset($databases['default']['default'])) {
$default_driver = $databases['default']['default']['driver'];
$default_options = $databases['default']['default'];
}
// Otherwise, use the database connection settings from the form input.
// For a non-interactive installation, this is derived from the original
// $settings array passed into install_drupal().
elseif (isset($form_state['input']['driver'])) {
$default_driver = $form_state['input']['driver'];
$default_options = $form_state['input'][$default_driver];
}
// If there is no database information at all yet, just 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(array($this, 'submitForm')),
);
$form['errors'] = array();
$form['settings_file'] = array('#type' => 'value', '#value' => $settings_file);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
$driver = $form_state['values']['driver'];
$database = $form_state['values'][$driver];
$drivers = drupal_get_database_types();
$reflection = new \ReflectionClass($drivers[$driver]);
$install_namespace = $reflection->getNamespaceName();
// Cut the trailing \Install from namespace.
$database['namespace'] = substr($install_namespace, 0, strrpos($install_namespace, '\\'));
$database['driver'] = $driver;
$form_state['storage']['database'] = $database;
$errors = install_database_errors($database, $form_state['values']['settings_file']);
foreach ($errors as $name => $message) {
$this->setFormError($name, $form_state, $message);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
global $install_state;
// Update global settings array and save.
$settings = array();
$database = $form_state['storage']['database'];
$settings['databases']['default']['default'] = (object) array(
'value' => $database,
'required' => TRUE,
);
$settings['settings']['hash_salt'] = (object) array(
'value' => Crypt::randomBytesBase64(55),
'required' => TRUE,
);
// Remember the profile which was used.
$settings['settings']['install_profile'] = (object) array(
'value' => $install_state['parameters']['profile'],
'required' => TRUE,
);
drupal_rewrite_settings($settings);
// Add the config directories to settings.php.
drupal_install_config_directories();
// Indicate that the settings file has been verified, and check the database
// for the last completed task, now that we have a valid connection. This
// last step is important since we want to trigger an error if the new
// database already has Drupal installed.
$install_state['settings_verified'] = TRUE;
$install_state['config_verified'] = TRUE;
$install_state['database_verified'] = TRUE;
$install_state['completed_task'] = install_verify_completed_task();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment