3 namespace Drupal\Core\Installer\Form;
5 use Drupal\Core\Extension\ModuleInstallerInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Locale\CountryManagerInterface;
9 use Drupal\Core\State\StateInterface;
10 use Drupal\user\UserStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides the site configuration form.
18 class SiteConfigureForm extends ConfigFormBase {
30 * @var \Drupal\user\UserStorageInterface
32 protected $userStorage;
37 * @var \Drupal\Core\State\StateInterface
42 * The module installer.
44 * @var \Drupal\Core\Extension\ModuleInstallerInterface
46 protected $moduleInstaller;
49 * The country manager.
51 * @var \Drupal\Core\Locale\CountryManagerInterface
53 protected $countryManager;
63 * Constructs a new SiteConfigureForm.
67 * @param string $site_path
69 * @param \Drupal\user\UserStorageInterface $user_storage
71 * @param \Drupal\Core\State\StateInterface $state
73 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
74 * The module installer.
75 * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
76 * The country manager.
78 public function __construct($root, $site_path, UserStorageInterface $user_storage, StateInterface $state, ModuleInstallerInterface $module_installer, CountryManagerInterface $country_manager) {
80 $this->sitePath = $site_path;
81 $this->userStorage = $user_storage;
82 $this->state = $state;
83 $this->moduleInstaller = $module_installer;
84 $this->countryManager = $country_manager;
90 public static function create(ContainerInterface $container) {
92 $container->get('app.root'),
93 $container->get('site.path'),
94 $container->get('entity.manager')->getStorage('user'),
95 $container->get('state'),
96 $container->get('module_installer'),
97 $container->get('country_manager')
104 public function getFormId() {
105 return 'install_configure_form';
111 protected function getEditableConfigNames() {
122 public function buildForm(array $form, FormStateInterface $form_state) {
123 $form['#title'] = $this->t('Configure site');
125 // Warn about settings.php permissions risk
126 $settings_dir = $this->sitePath;
127 $settings_file = $settings_dir . '/settings.php';
128 // Check that $_POST is empty so we only show this message when the form is
129 // first displayed, not on the next page after it is submitted. (We do not
130 // want to repeat it multiple times because it is a general warning that is
131 // not related to the rest of the installation process; it would also be
132 // especially out of place on the last page of the installer, where it would
133 // distract from the message that the Drupal installation has completed
135 $post_params = $this->getRequest()->request->all();
136 if (empty($post_params) && (!drupal_verify_install_file($this->root . '/' . $settings_file, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE) || !drupal_verify_install_file($this->root . '/' . $settings_dir, FILE_NOT_WRITABLE, 'dir'))) {
137 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>.', ['%dir' => $settings_dir, '%file' => $settings_file, ':handbook_url' => 'https://www.drupal.org/server-permissions']), 'warning');
140 $form['#attached']['library'][] = 'system/drupal.system';
141 // Add JavaScript time zone detection.
142 $form['#attached']['library'][] = 'core/drupal.timezone';
143 // We add these strings as settings because JavaScript translation does not
144 // work during installation.
145 $form['#attached']['drupalSettings']['copyFieldValue']['edit-site-mail'] = ['edit-account-mail'];
147 $form['site_information'] = [
148 '#type' => 'fieldgroup',
149 '#title' => $this->t('Site information'),
151 $form['site_information']['site_name'] = [
152 '#type' => 'textfield',
153 '#title' => $this->t('Site name'),
157 $form['site_information']['site_mail'] = [
159 '#title' => $this->t('Site email address'),
160 '#default_value' => ini_get('sendmail_from'),
161 '#description' => $this->t("Automated emails, such as registration information, will be sent from this address. Use an address ending in your site's domain to help prevent these emails from being flagged as spam."),
166 $form['admin_account'] = [
167 '#type' => 'fieldgroup',
168 '#title' => $this->t('Site maintenance account'),
170 $form['admin_account']['account']['name'] = [
171 '#type' => 'textfield',
172 '#title' => $this->t('Username'),
173 '#maxlength' => USERNAME_MAX_LENGTH,
174 '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
176 '#attributes' => ['class' => ['username']],
178 $form['admin_account']['account']['pass'] = [
179 '#type' => 'password_confirm',
183 $form['admin_account']['account']['#tree'] = TRUE;
184 $form['admin_account']['account']['mail'] = [
186 '#title' => $this->t('Email address'),
190 $form['regional_settings'] = [
191 '#type' => 'fieldgroup',
192 '#title' => $this->t('Regional settings'),
194 $countries = $this->countryManager->getList();
195 $form['regional_settings']['site_default_country'] = [
197 '#title' => $this->t('Default country'),
198 '#empty_value' => '',
199 '#default_value' => $this->config('system.date')->get('country.default'),
200 '#options' => $countries,
201 '#description' => $this->t('Select the default country for the site.'),
204 $form['regional_settings']['date_default_timezone'] = [
206 '#title' => $this->t('Default time zone'),
207 // Use system timezone if set, but avoid throwing a warning in PHP >=5.4
208 '#default_value' => @date_default_timezone_get(),
209 '#options' => system_time_zones(NULL, TRUE),
210 '#description' => $this->t('By default, dates in this site will be displayed in the chosen time zone.'),
212 '#attributes' => ['class' => ['timezone-detect']],
215 $form['update_notifications'] = [
216 '#type' => 'fieldgroup',
217 '#title' => $this->t('Update notifications'),
218 '#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>.', [':drupal' => 'https://www.drupal.org']),
220 $form['update_notifications']['enable_update_status_module'] = [
221 '#type' => 'checkbox',
222 '#title' => $this->t('Check for updates automatically'),
223 '#default_value' => 1,
225 $form['update_notifications']['enable_update_status_emails'] = [
226 '#type' => 'checkbox',
227 '#title' => $this->t('Receive email notifications'),
228 '#default_value' => 1,
231 'input[name="enable_update_status_module"]' => ['checked' => TRUE],
236 $form['actions'] = ['#type' => 'actions'];
237 $form['actions']['submit'] = [
239 '#value' => $this->t('Save and continue'),
241 '#button_type' => 'primary',
250 public function validateForm(array &$form, FormStateInterface $form_state) {
251 if ($error = user_validate_name($form_state->getValue(['account', 'name']))) {
252 $form_state->setErrorByName('account][name', $error);
259 public function submitForm(array &$form, FormStateInterface $form_state) {
260 $this->config('system.site')
261 ->set('name', (string) $form_state->getValue('site_name'))
262 ->set('mail', (string) $form_state->getValue('site_mail'))
265 $this->config('system.date')
266 ->set('timezone.default', (string) $form_state->getValue('date_default_timezone'))
267 ->set('country.default', (string) $form_state->getValue('site_default_country'))
270 $account_values = $form_state->getValue('account');
272 // Enable update.module if this option was selected.
273 $update_status_module = $form_state->getValue('enable_update_status_module');
274 if ($update_status_module) {
275 $this->moduleInstaller->install(['file', 'update'], FALSE);
277 // Add the site maintenance account's email address to the list of
278 // addresses to be notified when updates are available, if selected.
279 $email_update_status_emails = $form_state->getValue('enable_update_status_emails');
280 if ($email_update_status_emails) {
281 // Reset the configuration factory so it is updated with the new module.
282 $this->resetConfigFactory();
283 $this->config('update.settings')->set('notification.emails', [$account_values['mail']])->save(TRUE);
287 // We precreated user 1 with placeholder values. Let's save the real values.
288 $account = $this->userStorage->load(1);
289 $account->init = $account->mail = $account_values['mail'];
290 $account->roles = $account->getRoles();
291 $account->activate();
292 $account->timezone = $form_state->getValue('date_default_timezone');
293 $account->pass = $account_values['pass'];
294 $account->name = $account_values['name'];
297 // Record when this install ran.
298 $this->state->set('install_time', $_SERVER['REQUEST_TIME']);