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\Site\Settings;
10 use Drupal\Core\State\StateInterface;
11 use Drupal\user\UserStorageInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Provides the site configuration form.
19 class SiteConfigureForm extends ConfigFormBase {
31 * @var \Drupal\user\UserStorageInterface
33 protected $userStorage;
38 * @var \Drupal\Core\State\StateInterface
43 * The module installer.
45 * @var \Drupal\Core\Extension\ModuleInstallerInterface
47 protected $moduleInstaller;
50 * The country manager.
52 * @var \Drupal\Core\Locale\CountryManagerInterface
54 protected $countryManager;
64 * Constructs a new SiteConfigureForm.
68 * @param string $site_path
70 * @param \Drupal\user\UserStorageInterface $user_storage
72 * @param \Drupal\Core\State\StateInterface $state
74 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
75 * The module installer.
76 * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
77 * The country manager.
79 public function __construct($root, $site_path, UserStorageInterface $user_storage, StateInterface $state, ModuleInstallerInterface $module_installer, CountryManagerInterface $country_manager) {
81 $this->sitePath = $site_path;
82 $this->userStorage = $user_storage;
83 $this->state = $state;
84 $this->moduleInstaller = $module_installer;
85 $this->countryManager = $country_manager;
91 public static function create(ContainerInterface $container) {
93 $container->get('app.root'),
94 $container->get('site.path'),
95 $container->get('entity.manager')->getStorage('user'),
96 $container->get('state'),
97 $container->get('module_installer'),
98 $container->get('country_manager')
105 public function getFormId() {
106 return 'install_configure_form';
112 protected function getEditableConfigNames() {
123 public function buildForm(array $form, FormStateInterface $form_state) {
124 global $install_state;
125 $form['#title'] = $this->t('Configure site');
127 // Warn about settings.php permissions risk
128 $settings_dir = $this->sitePath;
129 $settings_file = $settings_dir . '/settings.php';
130 // Check that $_POST is empty so we only show this message when the form is
131 // first displayed, not on the next page after it is submitted. (We do not
132 // want to repeat it multiple times because it is a general warning that is
133 // not related to the rest of the installation process; it would also be
134 // especially out of place on the last page of the installer, where it would
135 // distract from the message that the Drupal installation has completed
137 $post_params = $this->getRequest()->request->all();
138 if (empty($post_params) && (Settings::get('skip_permissions_hardening') || !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'))) {
139 $this->messenger()->addWarning(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']));
142 $form['#attached']['library'][] = 'system/drupal.system';
143 // Add JavaScript time zone detection.
144 $form['#attached']['library'][] = 'core/drupal.timezone';
145 // We add these strings as settings because JavaScript translation does not
146 // work during installation.
147 $form['#attached']['drupalSettings']['copyFieldValue']['edit-site-mail'] = ['edit-account-mail'];
149 $form['site_information'] = [
150 '#type' => 'fieldgroup',
151 '#title' => $this->t('Site information'),
152 '#access' => empty($install_state['config_install_path']),
154 $form['site_information']['site_name'] = [
155 '#type' => 'textfield',
156 '#title' => $this->t('Site name'),
159 '#access' => empty($install_state['config_install_path']),
161 $form['site_information']['site_mail'] = [
163 '#title' => $this->t('Site email address'),
164 '#default_value' => ini_get('sendmail_from'),
165 '#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."),
168 '#access' => empty($install_state['config_install_path']),
171 $form['admin_account'] = [
172 '#type' => 'fieldgroup',
173 '#title' => $this->t('Site maintenance account'),
175 $form['admin_account']['account']['name'] = [
176 '#type' => 'textfield',
177 '#title' => $this->t('Username'),
178 '#maxlength' => USERNAME_MAX_LENGTH,
179 '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
181 '#attributes' => ['class' => ['username']],
183 $form['admin_account']['account']['pass'] = [
184 '#type' => 'password_confirm',
188 $form['admin_account']['account']['#tree'] = TRUE;
189 $form['admin_account']['account']['mail'] = [
191 '#title' => $this->t('Email address'),
195 $form['regional_settings'] = [
196 '#type' => 'fieldgroup',
197 '#title' => $this->t('Regional settings'),
198 '#access' => empty($install_state['config_install_path']),
200 $countries = $this->countryManager->getList();
201 $form['regional_settings']['site_default_country'] = [
203 '#title' => $this->t('Default country'),
204 '#empty_value' => '',
205 '#default_value' => $this->config('system.date')->get('country.default'),
206 '#options' => $countries,
207 '#description' => $this->t('Select the default country for the site.'),
209 '#access' => empty($install_state['config_install_path']),
211 $form['regional_settings']['date_default_timezone'] = [
213 '#title' => $this->t('Default time zone'),
214 // Use system timezone if set, but avoid throwing a warning in PHP >=5.4
215 '#default_value' => @date_default_timezone_get(),
216 '#options' => system_time_zones(NULL, TRUE),
217 '#description' => $this->t('By default, dates in this site will be displayed in the chosen time zone.'),
219 '#attributes' => ['class' => ['timezone-detect']],
220 '#access' => empty($install_state['config_install_path']),
223 $form['update_notifications'] = [
224 '#type' => 'fieldgroup',
225 '#title' => $this->t('Update notifications'),
226 '#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']),
227 '#access' => empty($install_state['config_install_path']),
229 $form['update_notifications']['enable_update_status_module'] = [
230 '#type' => 'checkbox',
231 '#title' => $this->t('Check for updates automatically'),
232 '#default_value' => 1,
233 '#access' => empty($install_state['config_install_path']),
235 $form['update_notifications']['enable_update_status_emails'] = [
236 '#type' => 'checkbox',
237 '#title' => $this->t('Receive email notifications'),
238 '#default_value' => 1,
241 'input[name="enable_update_status_module"]' => ['checked' => TRUE],
244 '#access' => empty($install_state['config_install_path']),
247 $form['actions'] = ['#type' => 'actions'];
248 $form['actions']['submit'] = [
250 '#value' => $this->t('Save and continue'),
252 '#button_type' => 'primary',
261 public function validateForm(array &$form, FormStateInterface $form_state) {
262 if ($error = user_validate_name($form_state->getValue(['account', 'name']))) {
263 $form_state->setErrorByName('account][name', $error);
270 public function submitForm(array &$form, FormStateInterface $form_state) {
271 global $install_state;
273 if (empty($install_state['config_install_path'])) {
274 $this->config('system.site')
275 ->set('name', (string) $form_state->getValue('site_name'))
276 ->set('mail', (string) $form_state->getValue('site_mail'))
279 $this->config('system.date')
280 ->set('timezone.default', (string) $form_state->getValue('date_default_timezone'))
281 ->set('country.default', (string) $form_state->getValue('site_default_country'))
285 $account_values = $form_state->getValue('account');
287 // Enable update.module if this option was selected.
288 $update_status_module = $form_state->getValue('enable_update_status_module');
289 if (empty($install_state['config_install_path']) && $update_status_module) {
290 $this->moduleInstaller->install(['file', 'update'], FALSE);
292 // Add the site maintenance account's email address to the list of
293 // addresses to be notified when updates are available, if selected.
294 $email_update_status_emails = $form_state->getValue('enable_update_status_emails');
295 if ($email_update_status_emails) {
296 // Reset the configuration factory so it is updated with the new module.
297 $this->resetConfigFactory();
298 $this->config('update.settings')->set('notification.emails', [$account_values['mail']])->save(TRUE);
302 // We precreated user 1 with placeholder values. Let's save the real values.
303 $account = $this->userStorage->load(1);
304 $account->init = $account->mail = $account_values['mail'];
305 $account->roles = $account->getRoles();
306 $account->activate();
307 $account->timezone = $form_state->getValue('date_default_timezone');
308 $account->pass = $account_values['pass'];
309 $account->name = $account_values['name'];
312 // Record when this install ran.
313 $this->state->set('install_time', $_SERVER['REQUEST_TIME']);