Verified Commit c312cc11 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3343755 by kim.pepper, catch, smustgrave, longwave: Simplify TimeZoneFormHelper

parent 45e7bcd5
Loading
Loading
Loading
Loading
+36 −23
Original line number Diff line number Diff line
@@ -18,15 +18,13 @@ class TimeZoneFormHelper {
   *
   * @param bool $blank
   *   (optional) If TRUE, prepend an empty time zone option to the array.
   * @param bool $grouped
   *   (optional) Whether the timezones should be grouped by region.
   *
   * @return array
   *   An array or nested array containing time zones, keyed by the system name.
   *   The keys are valid time zone identifiers provided by
   *   \DateTimeZone::listIdentifiers()
   */
  public static function getOptionsList(bool $blank = FALSE, bool $grouped = FALSE): array {
  public static function getOptionsList(bool $blank = FALSE): array {
    $zonelist = \DateTimeZone::listIdentifiers();
    $zones = $blank ? ['' => new TranslatableMarkup('- None selected -')] : [];
    foreach ($zonelist as $zone) {
@@ -34,7 +32,26 @@ public static function getOptionsList(bool $blank = FALSE, bool $grouped = FALSE
    }
    // Sort the translated time zones alphabetically.
    asort($zones);
    if ($grouped) {
    return $zones;
  }

  /**
   * Generate an array of time zones names grouped by region.
   *
   * This method retrieves the list of IANA time zones names that PHP is
   * configured to use, for display to users. It does not return the backward
   * compatible names (i.e., the ones defined in the back-zone file).
   *
   * @param bool $blank
   *   (optional) If TRUE, prepend an empty time zone option to the array.
   *
   * @return array
   *   An nested array containing time zones, keyed by the system name. The keys
   *   are valid time zone identifiers provided by
   *   \DateTimeZone::listIdentifiers()
   */
  public static function getOptionsListByRegion(bool $blank = FALSE): array {
    $zones = static::getOptionsList($blank);
    $grouped_zones = [];
    foreach ($zones as $key => $value) {
      $split = explode('/', $value);
@@ -52,11 +69,7 @@ public static function getOptionsList(bool $blank = FALSE, bool $grouped = FALSE
        asort($grouped_zones[$key]);
      }
    }
      $zones = $grouped_zones;
    }

    return $zones;

    return $grouped_zones;
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -201,7 +201,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
    $form['timezone'] = [
      '#type' => 'select',
      '#title' => $this->t('Time zone'),
      '#options' => ['' => $this->t('- Default site/user time zone -')] + TimeZoneFormHelper::getOptionsList(FALSE, TRUE),
      '#options' => ['' => $this->t('- Default site/user time zone -')] + TimeZoneFormHelper::getOptionsListByRegion(),
      '#default_value' => $this->getSetting('timezone'),
    ];

+1 −1
Original line number Diff line number Diff line
@@ -208,7 +208,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#type' => 'select',
      '#title' => $this->t('Default time zone'),
      '#default_value' => $default_timezone,
      '#options' => TimeZoneFormHelper::getOptionsList(FALSE, TRUE),
      '#options' => TimeZoneFormHelper::getOptionsListByRegion(),
      '#weight' => 5,
      '#attributes' => ['class' => ['timezone-detect']],
      '#access' => empty($install_state['config_install_path']),
+1 −1
Original line number Diff line number Diff line
@@ -98,7 +98,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
      '#type' => 'select',
      '#title' => $this->t('Time zone override'),
      '#description' => $this->t('The time zone selected here will always be used'),
      '#options' => TimeZoneFormHelper::getOptionsList(TRUE, TRUE),
      '#options' => TimeZoneFormHelper::getOptionsListByRegion(TRUE),
      '#default_value' => $this->getSetting('timezone_override'),
    ];

+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
    $system_date = $this->config('system.date');

    // Date settings:
    $zones = TimeZoneFormHelper::getOptionsList(FALSE, TRUE);
    $zones = TimeZoneFormHelper::getOptionsListByRegion();

    $form['locale'] = [
      '#type' => 'details',
Loading