3 namespace Drupal\system\Form;
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;
12 * Configure regional settings for this site.
14 class RegionalForm extends ConfigFormBase {
17 * The country manager.
19 * @var \Drupal\Core\Locale\CountryManagerInterface
21 protected $countryManager;
24 * Constructs a RegionalForm object.
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.
31 public function __construct(ConfigFactoryInterface $config_factory, CountryManagerInterface $country_manager) {
32 parent::__construct($config_factory);
33 $this->countryManager = $country_manager;
39 public static function create(ContainerInterface $container) {
41 $container->get('config.factory'),
42 $container->get('country_manager')
49 public function getFormId() {
50 return 'system_regional_settings';
56 protected function getEditableConfigNames() {
57 return ['system.date'];
63 public function buildForm(array $form, FormStateInterface $form_state) {
64 $countries = $this->countryManager->getList();
65 $system_date = $this->config('system.date');
68 $zones = system_time_zones();
72 '#title' => t('Locale'),
76 $form['locale']['site_default_country'] = [
78 '#title' => t('Default country'),
80 '#default_value' => $system_date->get('country.default'),
81 '#options' => $countries,
82 '#attributes' => ['class' => ['country-detect']],
85 $form['locale']['date_first_day'] = [
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')],
94 '#title' => t('Time zones'),
98 $form['timezone']['date_default_timezone'] = [
100 '#title' => t('Default time zone'),
101 '#default_value' => $system_date->get('timezone.default') ?: date_default_timezone_get(),
102 '#options' => $zones,
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,
112 $form['timezone']['configurable_timezones_wrapper'] = [
113 '#type' => 'container',
115 // Hide the user configured timezone settings when users are forced to use
116 // the default setting.
118 'input[name="configurable_timezones"]' => ['checked' => FALSE],
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.')
129 $form['timezone']['configurable_timezones_wrapper']['user_default_timezone'] = [
131 '#title' => t('Time zone for new users'),
132 '#default_value' => $system_date->get('timezone.user.default'),
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'),
138 '#description' => t('Only applied if users may set their own time zone.')
141 return parent::buildForm($form, $form_state);
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'))
157 parent::submitForm($form, $form_state);