Commit 16f07ea5 authored by webchick's avatar webchick
Browse files

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'];
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Installer\Form\SiteConfigureForm.
*/
namespace Drupal\Core\Installer\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the site configuration form.
*/
class SiteConfigureForm extends FormBase {
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The country manager.
*
* @var \Drupal\Core\Locale\CountryManagerInterface
*/
protected $countryManager;
/**
* Constructs a new SiteConfigureForm.
*
* @param \Drupal\user\UserStorageInterface $user_storage
* The user storage.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
* The country manager.
*/
public function __construct(UserStorageInterface $user_storage, StateInterface $state, ModuleHandlerInterface $module_handler, CountryManagerInterface $country_manager) {
$this->userStorage = $user_storage;
$this->state = $state;
$this->moduleHandler = $module_handler;
$this->countryManager = $country_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')->getStorage('user'),
$container->get('state'),
$container->get('module_handler'),
$container->get('country_manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'install_configure_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form['#title'] = $this->t('Configure site');
// Warn about settings.php permissions risk
$settings_dir = conf_path();
$settings_file = $settings_dir . '/settings.php';
// Check that $_POST is empty so we only show this message when the form is
// first displayed, not on the next page after it is submitted. (We do not
// want to repeat it multiple times because it is a general warning that is
// not related to the rest of the installation process; it would also be
// especially out of place on the last page of the installer, where it would
// distract from the message that the Drupal installation has completed
// successfully.)
$post_params = $this->getRequest()->request->all();
if (empty($post_params) && (!drupal_verify_install_file(DRUPAL_ROOT . '/' . $settings_file, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE) || !drupal_verify_install_file(DRUPAL_ROOT . '/' . $settings_dir, FILE_NOT_WRITABLE, 'dir'))) {
drupal_set_message(t('All necessary changes to %dir and %file have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, consult the <a href="@handbook_url">online handbook</a>.', array('%dir' => $settings_dir, '%file' => $settings_file, '@handbook_url' => 'http://drupal.org/server-permissions')), 'warning');
}
$form['#attached']['library'][] = 'system/drupal.system';
// Add JavaScript time zone detection.
$form['#attached']['library'][] = 'core/drupal.timezone';
// We add these strings as settings because JavaScript translation does not
// work during installation.
$js = array('copyFieldValue' => array('edit-site-mail' => array('edit-account-mail')));
$form['#attached']['js'][] = array('data' => $js, 'type' => 'setting');
// Cache a fully-built schema. This is necessary for any invocation of
// index.php because: (1) setting cache table entries requires schema
// information, (2) that occurs during bootstrap before any module are
// loaded, so (3) if there is no cached schema, drupal_get_schema() will
// try to generate one but with no loaded modules will return nothing.
//
// @todo Move this to the 'install_finished' task?
drupal_get_schema(NULL, TRUE);
$form['site_information'] = array(
'#type' => 'fieldgroup',
'#title' => $this->t('Site information'),
);
$form['site_information']['site_name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Site name'),
'#required' => TRUE,
'#weight' => -20,
);
$form['site_information']['site_mail'] = array(
'#type' => 'email',
'#title' => $this->t('Site e-mail address'),
'#default_value' => ini_get('sendmail_from'),
'#description' => $this->t("Automated e-mails, such as registration information, will be sent from this address. Use an address ending in your site's domain to help prevent these e-mails from being flagged as spam."),
'#required' => TRUE,
'#weight' => -15,
);
$form['admin_account'] = array(
'#type' => 'fieldgroup',
'#title' => $this->t('Site maintenance account'),
);
$form['admin_account']['account']['#tree'] = TRUE;
$form['admin_account']['account']['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Username'),
'#maxlength' => USERNAME_MAX_LENGTH,
'#description' => $this->t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.'),
'#required' => TRUE,
'#weight' => -10,
'#attributes' => array('class' => array('username')),
);
$form['admin_account']['account']['mail'] = array(
'#type' => 'email',
'#title' => $this->t('E-mail address'),
'#required' => TRUE,
'#weight' => -5,
);
$form['admin_account']['account']['pass'] = array(
'#type' => 'password_confirm',
'#required' => TRUE,
'#size' => 25,
'#weight' => 0,
);
$form['regional_settings'] = array(
'#type' => 'fieldgroup',
'#title' => $this->t('Regional settings'),
);
$countries = $this->countryManager->getList();
$form['regional_settings']['site_default_country'] = array(
'#type' => 'select',
'#title' => $this->t('Default country'),
'#empty_value' => '',
'#default_value' => $this->config('system.date')->get('country.default'),
'#options' => $countries,
'#description' => $this->t('Select the default country for the site.'),
'#weight' => 0,
);
$form['regional_settings']['date_default_timezone'] = array(
'#type' => 'select',
'#title' => $this->t('Default time zone'),
'#default_value' => date_default_timezone_get(),
'#options' => system_time_zones(),
'#description' => $this->t('By default, dates in this site will be displayed in the chosen time zone.'),
'#weight' => 5,
'#attributes' => array('class' => array('timezone-detect')),
);
$form['update_notifications'] = array(
'#type' => 'fieldgroup',
'#title' => $this->t('Update notifications'),
);
$form['update_notifications']['update_status_module'] = array(
'#type' => 'checkboxes',
'#title' => $this->t('Update notifications'),
'#options' => array(
1 => $this->t('Check for updates automatically'),
2 => $this->t('Receive e-mail notifications'),
),
'#default_value' => array(1, 2),
'#description' => $this->t('The system will notify you when updates and important security releases are available for installed components. Anonymous information about your site is sent to <a href="@drupal">Drupal.org</a>.', array('@drupal' => 'http://drupal.org')),
'#weight' => 15,
);
$form['update_notifications']['update_status_module'][2] = array(
'#states' => array(
'visible' => array(
'input[name="update_status_module[1]"]' => array('checked' => TRUE),
),
),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save and continue'),
'#weight' => 15,
'#button_type' => 'primary',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
if ($error = user_validate_name($form_state['values']['account']['name'])) {
$this->setFormError('account][name', $form_state, $error);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$this->config('system.site')
->set('name', $form_state['values']['site_name'])
->set('mail', $form_state['values']['site_mail'])
->save();
$this->config('system.date')
->set('timezone.default', $form_state['values']['date_default_timezone'])
->set('country.default', $form_state['values']['site_default_country'])
->save();
// Enable update.module if this option was selected.
if ($form_state['values']['update_status_module'][1]) {
$this->moduleHandler->install(array('file', 'update'), FALSE);
// Add the site maintenance account's email address to the list of
// addresses to be notified when updates are available, if selected.
if ($form_state['values']['update_status_module'][2]) {
$this->config('update.settings')->set('notification.emails', array($form_state['values']['account']['mail']))->save();
}
}
// We precreated user 1 with placeholder values. Let's save the real values.
$account = $this->userStorage->load(1);
$account->init = $account->mail = $form_state['values']['account']['mail'];
$account->roles = $account->getRoles();
$account->activate();
$account->timezone = $form_state['values']['date_default_timezone'];
$account->pass = $form_state['values']['account']['pass'];
$account->name = $form_state['values']['account']['name'];
$account->save();
// Record when this install ran.
$this->state->set('install_time', $_SERVER['REQUEST_TIME']);
}
}
<?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}