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.
16 class RegionalForm extends ConfigFormBase {
19 * The country manager.
21 * @var \Drupal\Core\Locale\CountryManagerInterface
23 protected $countryManager;
26 * Constructs a RegionalForm object.
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.
33 public function __construct(ConfigFactoryInterface $config_factory, CountryManagerInterface $country_manager) {
34 parent::__construct($config_factory);
35 $this->countryManager = $country_manager;
41 public static function create(ContainerInterface $container) {
43 $container->get('config.factory'),
44 $container->get('country_manager')
51 public function getFormId() {
52 return 'system_regional_settings';
58 protected function getEditableConfigNames() {
59 return ['system.date'];
65 public function buildForm(array $form, FormStateInterface $form_state) {
66 $countries = $this->countryManager->getList();
67 $system_date = $this->config('system.date');
70 $zones = system_time_zones(NULL, TRUE);
74 '#title' => t('Locale'),
78 $form['locale']['site_default_country'] = [
80 '#title' => t('Default country'),
82 '#default_value' => $system_date->get('country.default'),
83 '#options' => $countries,
84 '#attributes' => ['class' => ['country-detect']],
87 $form['locale']['date_first_day'] = [
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')],
96 '#title' => t('Time zones'),
100 $form['timezone']['date_default_timezone'] = [
102 '#title' => t('Default time zone'),
103 '#default_value' => $system_date->get('timezone.default') ?: date_default_timezone_get(),
104 '#options' => $zones,
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,
114 $form['timezone']['configurable_timezones_wrapper'] = [
115 '#type' => 'container',
117 // Hide the user configured timezone settings when users are forced to use
118 // the default setting.
120 'input[name="configurable_timezones"]' => ['checked' => FALSE],
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.'),
131 $form['timezone']['configurable_timezones_wrapper']['user_default_timezone'] = [
133 '#title' => t('Time zone for new users'),
134 '#default_value' => $system_date->get('timezone.user.default'),
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'),
140 '#description' => t('Only applied if users may set their own time zone.'),
143 return parent::buildForm($form, $form_state);
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'))
159 parent::submitForm($form, $form_state);