RegionalForm.php 5.0 KB

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