Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Installer / Form / SiteConfigureForm.php
1 <?php
2
3 namespace Drupal\Core\Installer\Form;
4
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;
12
13 /**
14  * Provides the site configuration form.
15  *
16  * @internal
17  */
18 class SiteConfigureForm extends ConfigFormBase {
19
20   /**
21    * The site path.
22    *
23    * @var string
24    */
25   protected $sitePath;
26
27   /**
28    * The user storage.
29    *
30    * @var \Drupal\user\UserStorageInterface
31    */
32   protected $userStorage;
33
34   /**
35    * The state service.
36    *
37    * @var \Drupal\Core\State\StateInterface
38    */
39   protected $state;
40
41   /**
42    * The module installer.
43    *
44    * @var \Drupal\Core\Extension\ModuleInstallerInterface
45    */
46   protected $moduleInstaller;
47
48   /**
49    * The country manager.
50    *
51    * @var \Drupal\Core\Locale\CountryManagerInterface
52    */
53   protected $countryManager;
54
55   /**
56    * The app root.
57    *
58    * @var string
59    */
60   protected $root;
61
62   /**
63    * Constructs a new SiteConfigureForm.
64    *
65    * @param string $root
66    *   The app root.
67    * @param string $site_path
68    *   The site path.
69    * @param \Drupal\user\UserStorageInterface $user_storage
70    *   The user storage.
71    * @param \Drupal\Core\State\StateInterface $state
72    *   The state service.
73    * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
74    *   The module installer.
75    * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
76    *   The country manager.
77    */
78   public function __construct($root, $site_path, UserStorageInterface $user_storage, StateInterface $state, ModuleInstallerInterface $module_installer, CountryManagerInterface $country_manager) {
79     $this->root = $root;
80     $this->sitePath = $site_path;
81     $this->userStorage = $user_storage;
82     $this->state = $state;
83     $this->moduleInstaller = $module_installer;
84     $this->countryManager = $country_manager;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public static function create(ContainerInterface $container) {
91     return new static(
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')
98     );
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function getFormId() {
105     return 'install_configure_form';
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   protected function getEditableConfigNames() {
112     return [
113       'system.date',
114       'system.site',
115       'update.settings',
116     ];
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function buildForm(array $form, FormStateInterface $form_state) {
123     $form['#title'] = $this->t('Configure site');
124
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
134     // successfully.)
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');
138     }
139
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'];
146
147     $form['site_information'] = [
148       '#type' => 'fieldgroup',
149       '#title' => $this->t('Site information'),
150     ];
151     $form['site_information']['site_name'] = [
152       '#type' => 'textfield',
153       '#title' => $this->t('Site name'),
154       '#required' => TRUE,
155       '#weight' => -20,
156     ];
157     $form['site_information']['site_mail'] = [
158       '#type' => 'email',
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."),
162       '#required' => TRUE,
163       '#weight' => -15,
164     ];
165
166     $form['admin_account'] = [
167       '#type' => 'fieldgroup',
168       '#title' => $this->t('Site maintenance account'),
169     ];
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."),
175       '#required' => TRUE,
176       '#attributes' => ['class' => ['username']],
177     ];
178     $form['admin_account']['account']['pass'] = [
179       '#type' => 'password_confirm',
180       '#required' => TRUE,
181       '#size' => 25,
182     ];
183     $form['admin_account']['account']['#tree'] = TRUE;
184     $form['admin_account']['account']['mail'] = [
185       '#type' => 'email',
186       '#title' => $this->t('Email address'),
187       '#required' => TRUE,
188     ];
189
190     $form['regional_settings'] = [
191       '#type' => 'fieldgroup',
192       '#title' => $this->t('Regional settings'),
193     ];
194     $countries = $this->countryManager->getList();
195     $form['regional_settings']['site_default_country'] = [
196       '#type' => 'select',
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.'),
202       '#weight' => 0,
203     ];
204     $form['regional_settings']['date_default_timezone'] = [
205       '#type' => 'select',
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.'),
211       '#weight' => 5,
212       '#attributes' => ['class' => ['timezone-detect']],
213     ];
214
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']),
219     ];
220     $form['update_notifications']['enable_update_status_module'] = [
221       '#type' => 'checkbox',
222       '#title' => $this->t('Check for updates automatically'),
223       '#default_value' => 1,
224     ];
225     $form['update_notifications']['enable_update_status_emails'] = [
226       '#type' => 'checkbox',
227       '#title' => $this->t('Receive email notifications'),
228       '#default_value' => 1,
229       '#states' => [
230         'visible' => [
231           'input[name="enable_update_status_module"]' => ['checked' => TRUE],
232         ],
233       ],
234     ];
235
236     $form['actions'] = ['#type' => 'actions'];
237     $form['actions']['submit'] = [
238       '#type' => 'submit',
239       '#value' => $this->t('Save and continue'),
240       '#weight' => 15,
241       '#button_type' => 'primary',
242     ];
243
244     return $form;
245   }
246
247   /**
248    * {@inheritdoc}
249    */
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);
253     }
254   }
255
256   /**
257    * {@inheritdoc}
258    */
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'))
263       ->save(TRUE);
264
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'))
268       ->save(TRUE);
269
270     $account_values = $form_state->getValue('account');
271
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);
276
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);
284       }
285     }
286
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'];
295     $account->save();
296
297     // Record when this install ran.
298     $this->state->set('install_time', $_SERVER['REQUEST_TIME']);
299   }
300
301 }