Verified Commit 8cd02c7f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3437325 by narendraR, phenaproxima, alexpott, Wim Leers: Add validation...

Issue #3437325 by narendraR, phenaproxima, alexpott, Wim Leers: Add validation constraints to system.date
parent 8a67d672
Loading
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Core\Validation\Plugin\Validation\Constraint;

use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Validation\Attribute\Constraint;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraints\Choice;

#[Constraint(
  id: 'CountryCode',
  label: new TranslatableMarkup('CountryCode', [], ['context' => 'Validation']),
)]
class CountryCodeConstraint implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): Choice {
    $countries = $container->get(CountryManagerInterface::class)->getList();
    $configuration['choices'] = array_keys($countries);
    return new Choice($configuration);
  }

}
+2 −2
Original line number Diff line number Diff line
first_day: 0
country:
  default: ''
  default: null
timezone:
  default: ''
  default: null
  user:
    configurable: true
    default: 0
+24 −0
Original line number Diff line number Diff line
@@ -86,17 +86,28 @@ system.cron:
system.date:
  type: config_object
  label: 'Date settings'
  constraints:
    FullyValidatable: ~
  mapping:
    first_day:
      type: integer
      label: 'First day of week'
      constraints:
        Range:
          # @see \Drupal\system\Form\RegionalForm::buildForm()
          min: 0
          max: 6
    country:
      type: mapping
      label: 'Country'
      mapping:
        default:
          nullable: true
          type: string
          label: 'Default country'
          constraints:
            # @see \Drupal\system\Form\RegionalForm::buildForm()
            CountryCode: []
    timezone:
      type: mapping
      label: 'Time zone settings'
@@ -104,6 +115,11 @@ system.date:
        default:
          type: string
          label: 'Default time zone'
          nullable: true
          constraints:
            # @see \Drupal\system\Form\RegionalForm::buildForm()
            Choice:
              callback: 'DateTimeZone::listIdentifiers'
        user:
          type: mapping
          label: 'User'
@@ -114,6 +130,14 @@ system.date:
            default:
              type: integer
              label: 'Time zone for new users'
              constraints:
                # Time zone for new users can have one of the following values:
                # - UserInterface::TIMEZONE_DEFAULT
                # - UserInterface::TIMEZONE_EMPTY
                # - UserInterface::TIMEZONE_SELECT
                # @see \Drupal\user\UserInterface::TIMEZONE_*
                # @todo Update this to use enum in https://www.drupal.org/project/drupal/issues/3402178
                Choice: [0, 1, 2]
            warn:
              type: boolean
              label: 'Remind users at login if their time zone is not set'
+9 −10
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Core\Datetime\TimeZoneFormHelper;
use Drupal\Core\Form\ConfigTarget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\RedundantEditableConfigNamesTrait;
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -17,6 +18,7 @@
 * @internal
 */
class RegionalForm extends ConfigFormBase {
  use RedundantEditableConfigNamesTrait;

  /**
   * The country manager.
@@ -58,13 +60,6 @@ public function getFormId() {
    return 'system_regional_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['system.date'];
  }

  /**
   * {@inheritdoc}
   */
@@ -84,7 +79,11 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#type' => 'select',
      '#title' => $this->t('Default country'),
      '#empty_value' => '',
      '#config_target' => 'system.date:country.default',
      '#config_target' => new ConfigTarget(
        'system.date',
        'country.default',
        toConfig: fn(?string $value) => $value ?: NULL
      ),
      '#options' => $countries,
      '#attributes' => ['class' => ['country-detect']],
    ];
@@ -119,13 +118,13 @@ public function buildForm(array $form, FormStateInterface $form_state) {
  /**
   * Prepares the saved timezone.default property to be displayed in the form.
   *
   * @param string $value
   * @param string|null $value
   *   The value saved in config.
   *
   * @return string
   *   The value of the form element.
   */
  public static function loadDefaultTimeZone(string $value): string {
  public static function loadDefaultTimeZone(?string $value): string {
    return $value ?: date_default_timezone_get();
  }

+19 −0
Original line number Diff line number Diff line
@@ -315,3 +315,22 @@ function system_post_update_move_development_settings_to_keyvalue(): void {
  \Drupal::keyValue('development_settings')->setMultiple($development_settings);
  $state->deleteMultiple(array_keys($development_settings));
}

/**
 * Updates system.date config to NULL for empty country and timezone defaults.
 */
function system_post_update_convert_empty_country_and_timezone_settings_to_null(): void {
  $system_date_settings = \Drupal::configFactory()->getEditable('system.date');
  $changed = FALSE;
  if ($system_date_settings->get('country.default') === '') {
    $system_date_settings->set('country.default', NULL);
    $changed = TRUE;
  }
  if ($system_date_settings->get('timezone.default') === '') {
    $system_date_settings->set('timezone.default', NULL);
    $changed = TRUE;
  }
  if ($changed) {
    $system_date_settings->save();
  }
}
Loading