Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / src / Form / RegionalForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Locale\CountryManagerInterface;
8 use Drupal\Core\Form\ConfigFormBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Configure regional settings for this site.
13  *
14  * @internal
15  */
16 class RegionalForm extends ConfigFormBase {
17
18   /**
19    * The country manager.
20    *
21    * @var \Drupal\Core\Locale\CountryManagerInterface
22    */
23   protected $countryManager;
24
25   /**
26    * Constructs a RegionalForm object.
27    *
28    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
29    *   The factory for configuration objects.
30    * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
31    *   The country manager.
32    */
33   public function __construct(ConfigFactoryInterface $config_factory, CountryManagerInterface $country_manager) {
34     parent::__construct($config_factory);
35     $this->countryManager = $country_manager;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container) {
42     return new static(
43       $container->get('config.factory'),
44       $container->get('country_manager')
45     );
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function getFormId() {
52     return 'system_regional_settings';
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   protected function getEditableConfigNames() {
59     return ['system.date'];
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function buildForm(array $form, FormStateInterface $form_state) {
66     $countries = $this->countryManager->getList();
67     $system_date = $this->config('system.date');
68
69     // Date settings:
70     $zones = system_time_zones(NULL, TRUE);
71
72     $form['locale'] = [
73       '#type' => 'details',
74       '#title' => t('Locale'),
75       '#open' => TRUE,
76     ];
77
78     $form['locale']['site_default_country'] = [
79       '#type' => 'select',
80       '#title' => t('Default country'),
81       '#empty_value' => '',
82       '#default_value' => $system_date->get('country.default'),
83       '#options' => $countries,
84       '#attributes' => ['class' => ['country-detect']],
85     ];
86
87     $form['locale']['date_first_day'] = [
88       '#type' => 'select',
89       '#title' => t('First day of week'),
90       '#default_value' => $system_date->get('first_day'),
91       '#options' => [0 => t('Sunday'), 1 => t('Monday'), 2 => t('Tuesday'), 3 => t('Wednesday'), 4 => t('Thursday'), 5 => t('Friday'), 6 => t('Saturday')],
92     ];
93
94     $form['timezone'] = [
95       '#type' => 'details',
96       '#title' => t('Time zones'),
97       '#open' => TRUE,
98     ];
99
100     $form['timezone']['date_default_timezone'] = [
101       '#type' => 'select',
102       '#title' => t('Default time zone'),
103       '#default_value' => $system_date->get('timezone.default') ?: date_default_timezone_get(),
104       '#options' => $zones,
105     ];
106
107     $configurable_timezones = $system_date->get('timezone.user.configurable');
108     $form['timezone']['configurable_timezones'] = [
109       '#type' => 'checkbox',
110       '#title' => t('Users may set their own time zone'),
111       '#default_value' => $configurable_timezones,
112     ];
113
114     $form['timezone']['configurable_timezones_wrapper'] = [
115       '#type' => 'container',
116       '#states' => [
117         // Hide the user configured timezone settings when users are forced to use
118         // the default setting.
119         'invisible' => [
120           'input[name="configurable_timezones"]' => ['checked' => FALSE],
121         ],
122       ],
123     ];
124     $form['timezone']['configurable_timezones_wrapper']['empty_timezone_message'] = [
125       '#type' => 'checkbox',
126       '#title' => t('Remind users at login if their time zone is not set'),
127       '#default_value' => $system_date->get('timezone.user.warn'),
128       '#description' => t('Only applied if users may set their own time zone.'),
129     ];
130
131     $form['timezone']['configurable_timezones_wrapper']['user_default_timezone'] = [
132       '#type' => 'radios',
133       '#title' => t('Time zone for new users'),
134       '#default_value' => $system_date->get('timezone.user.default'),
135       '#options' => [
136         DRUPAL_USER_TIMEZONE_DEFAULT => t('Default time zone'),
137         DRUPAL_USER_TIMEZONE_EMPTY   => t('Empty time zone'),
138         DRUPAL_USER_TIMEZONE_SELECT  => t('Users may set their own time zone at registration'),
139       ],
140       '#description' => t('Only applied if users may set their own time zone.'),
141     ];
142
143     return parent::buildForm($form, $form_state);
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function submitForm(array &$form, FormStateInterface $form_state) {
150     $this->config('system.date')
151       ->set('country.default', $form_state->getValue('site_default_country'))
152       ->set('first_day', $form_state->getValue('date_first_day'))
153       ->set('timezone.default', $form_state->getValue('date_default_timezone'))
154       ->set('timezone.user.configurable', $form_state->getValue('configurable_timezones'))
155       ->set('timezone.user.warn', $form_state->getValue('empty_timezone_message'))
156       ->set('timezone.user.default', $form_state->getValue('user_default_timezone'))
157       ->save();
158
159     parent::submitForm($form, $form_state);
160   }
161
162 }