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.
16 class SiteConfigureForm extends ConfigFormBase {
28 * @var \Drupal\user\UserStorageInterface
30 protected $userStorage;
35 * @var \Drupal\Core\State\StateInterface
40 * The module installer.
42 * @var \Drupal\Core\Extension\ModuleInstallerInterface
44 protected $moduleInstaller;
47 * The country manager.
49 * @var \Drupal\Core\Locale\CountryManagerInterface
51 protected $countryManager;
61 * Constructs a new SiteConfigureForm.
65 * @param string $site_path
67 * @param \Drupal\user\UserStorageInterface $user_storage
69 * @param \Drupal\Core\State\StateInterface $state
71 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
72 * The module installer.
73 * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
74 * The country manager.
76 public function __construct($root, $site_path, UserStorageInterface $user_storage, StateInterface $state, ModuleInstallerInterface $module_installer, CountryManagerInterface $country_manager) {
78 $this->sitePath = $site_path;
79 $this->userStorage = $user_storage;
80 $this->state = $state;
81 $this->moduleInstaller = $module_installer;
82 $this->countryManager = $country_manager;
88 public static function create(ContainerInterface $container) {
90 $container->get('app.root'),
91 $container->get('site.path'),
92 $container->get('entity.manager')->getStorage('user'),
93 $container->get('state'),
94 $container->get('module_installer'),
95 $container->get('country_manager')
102 public function getFormId() {
103 return 'install_configure_form';
109 protected function getEditableConfigNames() {
120 public function buildForm(array $form, FormStateInterface $form_state) {
121 $form['#title'] = $this->t('Configure site');
123 // Warn about settings.php permissions risk
124 $settings_dir = $this->sitePath;
125 $settings_file = $settings_dir . '/settings.php';
126 // Check that $_POST is empty so we only show this message when the form is
127 // first displayed, not on the next page after it is submitted. (We do not
128 // want to repeat it multiple times because it is a general warning that is
129 // not related to the rest of the installation process; it would also be
130 // especially out of place on the last page of the installer, where it would
131 // distract from the message that the Drupal installation has completed
133 $post_params = $this->getRequest()->request->all();
134 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'))) {
135 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');
138 $form['#attached']['library'][] = 'system/drupal.system';
139 // Add JavaScript time zone detection.
140 $form['#attached']['library'][] = 'core/drupal.timezone';
141 // We add these strings as settings because JavaScript translation does not
142 // work during installation.
143 $form['#attached']['drupalSettings']['copyFieldValue']['edit-site-mail'] = ['edit-account-mail'];
145 $form['site_information'] = [
146 '#type' => 'fieldgroup',
147 '#title' => $this->t('Site information'),
149 $form['site_information']['site_name'] = [
150 '#type' => 'textfield',
151 '#title' => $this->t('Site name'),
155 $form['site_information']['site_mail'] = [
157 '#title' => $this->t('Site email address'),
158 '#default_value' => ini_get('sendmail_from'),
159 '#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."),
164 $form['admin_account'] = [
165 '#type' => 'fieldgroup',
166 '#title' => $this->t('Site maintenance account'),
168 $form['admin_account']['account']['name'] = [
169 '#type' => 'textfield',
170 '#title' => $this->t('Username'),
171 '#maxlength' => USERNAME_MAX_LENGTH,
172 '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
174 '#attributes' => ['class' => ['username']],
176 $form['admin_account']['account']['pass'] = [
177 '#type' => 'password_confirm',
181 $form['admin_account']['account']['#tree'] = TRUE;
182 $form['admin_account']['account']['mail'] = [
184 '#title' => $this->t('Email address'),
188 $form['regional_settings'] = [
189 '#type' => 'fieldgroup',
190 '#title' => $this->t('Regional settings'),
192 $countries = $this->countryManager->getList();
193 $form['regional_settings']['site_default_country'] = [
195 '#title' => $this->t('Default country'),
196 '#empty_value' => '',
197 '#default_value' => $this->config('system.date')->get('country.default'),
198 '#options' => $countries,
199 '#description' => $this->t('Select the default country for the site.'),
202 $form['regional_settings']['date_default_timezone'] = [
204 '#title' => $this->t('Default time zone'),
205 // Use system timezone if set, but avoid throwing a warning in PHP >=5.4
206 '#default_value' => @date_default_timezone_get(),
207 '#options' => system_time_zones(),
208 '#description' => $this->t('By default, dates in this site will be displayed in the chosen time zone.'),
210 '#attributes' => ['class' => ['timezone-detect']],
213 $form['update_notifications'] = [
214 '#type' => 'fieldgroup',
215 '#title' => $this->t('Update notifications'),
216 '#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']),
218 $form['update_notifications']['enable_update_status_module'] = [
219 '#type' => 'checkbox',
220 '#title' => $this->t('Check for updates automatically'),
221 '#default_value' => 1,
223 $form['update_notifications']['enable_update_status_emails'] = [
224 '#type' => 'checkbox',
225 '#title' => $this->t('Receive email notifications'),
226 '#default_value' => 1,
229 'input[name="enable_update_status_module"]' => ['checked' => TRUE],
234 $form['actions'] = ['#type' => 'actions'];
235 $form['actions']['submit'] = [
237 '#value' => $this->t('Save and continue'),
239 '#button_type' => 'primary',
248 public function validateForm(array &$form, FormStateInterface $form_state) {
249 if ($error = user_validate_name($form_state->getValue(['account', 'name']))) {
250 $form_state->setErrorByName('account][name', $error);
257 public function submitForm(array &$form, FormStateInterface $form_state) {
258 $this->config('system.site')
259 ->set('name', (string) $form_state->getValue('site_name'))
260 ->set('mail', (string) $form_state->getValue('site_mail'))
263 $this->config('system.date')
264 ->set('timezone.default', (string) $form_state->getValue('date_default_timezone'))
265 ->set('country.default', (string) $form_state->getValue('site_default_country'))
268 $account_values = $form_state->getValue('account');
270 // Enable update.module if this option was selected.
271 $update_status_module = $form_state->getValue('enable_update_status_module');
272 if ($update_status_module) {
273 $this->moduleInstaller->install(['file', 'update'], FALSE);
275 // Add the site maintenance account's email address to the list of
276 // addresses to be notified when updates are available, if selected.
277 $email_update_status_emails = $form_state->getValue('enable_update_status_emails');
278 if ($email_update_status_emails) {
279 // Reset the configuration factory so it is updated with the new module.
280 $this->resetConfigFactory();
281 $this->config('update.settings')->set('notification.emails', [$account_values['mail']])->save(TRUE);
285 // We precreated user 1 with placeholder values. Let's save the real values.
286 $account = $this->userStorage->load(1);
287 $account->init = $account->mail = $account_values['mail'];
288 $account->roles = $account->getRoles();
289 $account->activate();
290 $account->timezone = $form_state->getValue('date_default_timezone');
291 $account->pass = $account_values['pass'];
292 $account->name = $account_values['name'];
295 // Record when this install ran.
296 $this->state->set('install_time', $_SERVER['REQUEST_TIME']);