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