Issue #3343755 by kim.pepper, catch, smustgrave, longwave: Simplify TimeZoneFormHelper
parent
45e7bcd530
commit
c312cc11bf
|
@ -18,15 +18,13 @@ class TimeZoneFormHelper {
|
||||||
*
|
*
|
||||||
* @param bool $blank
|
* @param bool $blank
|
||||||
* (optional) If TRUE, prepend an empty time zone option to the array.
|
* (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
|
* @return array
|
||||||
* An array or nested array containing time zones, keyed by the system name.
|
* An array or nested array containing time zones, keyed by the system name.
|
||||||
* The keys are valid time zone identifiers provided by
|
* The keys are valid time zone identifiers provided by
|
||||||
* \DateTimeZone::listIdentifiers()
|
* \DateTimeZone::listIdentifiers()
|
||||||
*/
|
*/
|
||||||
public static function getOptionsList(bool $blank = FALSE, bool $grouped = FALSE): array {
|
public static function getOptionsList(bool $blank = FALSE): array {
|
||||||
$zonelist = \DateTimeZone::listIdentifiers();
|
$zonelist = \DateTimeZone::listIdentifiers();
|
||||||
$zones = $blank ? ['' => new TranslatableMarkup('- None selected -')] : [];
|
$zones = $blank ? ['' => new TranslatableMarkup('- None selected -')] : [];
|
||||||
foreach ($zonelist as $zone) {
|
foreach ($zonelist as $zone) {
|
||||||
|
@ -34,7 +32,26 @@ class TimeZoneFormHelper {
|
||||||
}
|
}
|
||||||
// Sort the translated time zones alphabetically.
|
// Sort the translated time zones alphabetically.
|
||||||
asort($zones);
|
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 = [];
|
$grouped_zones = [];
|
||||||
foreach ($zones as $key => $value) {
|
foreach ($zones as $key => $value) {
|
||||||
$split = explode('/', $value);
|
$split = explode('/', $value);
|
||||||
|
@ -52,11 +69,7 @@ class TimeZoneFormHelper {
|
||||||
asort($grouped_zones[$key]);
|
asort($grouped_zones[$key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$zones = $grouped_zones;
|
return $grouped_zones;
|
||||||
}
|
|
||||||
|
|
||||||
return $zones;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -201,7 +201,7 @@ class TimestampFormatter extends FormatterBase {
|
||||||
$form['timezone'] = [
|
$form['timezone'] = [
|
||||||
'#type' => 'select',
|
'#type' => 'select',
|
||||||
'#title' => $this->t('Time zone'),
|
'#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'),
|
'#default_value' => $this->getSetting('timezone'),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -208,7 +208,7 @@ class SiteConfigureForm extends ConfigFormBase {
|
||||||
'#type' => 'select',
|
'#type' => 'select',
|
||||||
'#title' => $this->t('Default time zone'),
|
'#title' => $this->t('Default time zone'),
|
||||||
'#default_value' => $default_timezone,
|
'#default_value' => $default_timezone,
|
||||||
'#options' => TimeZoneFormHelper::getOptionsList(FALSE, TRUE),
|
'#options' => TimeZoneFormHelper::getOptionsListByRegion(),
|
||||||
'#weight' => 5,
|
'#weight' => 5,
|
||||||
'#attributes' => ['class' => ['timezone-detect']],
|
'#attributes' => ['class' => ['timezone-detect']],
|
||||||
'#access' => empty($install_state['config_install_path']),
|
'#access' => empty($install_state['config_install_path']),
|
||||||
|
|
|
@ -98,7 +98,7 @@ abstract class DateTimeFormatterBase extends FormatterBase {
|
||||||
'#type' => 'select',
|
'#type' => 'select',
|
||||||
'#title' => $this->t('Time zone override'),
|
'#title' => $this->t('Time zone override'),
|
||||||
'#description' => $this->t('The time zone selected here will always be used'),
|
'#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'),
|
'#default_value' => $this->getSetting('timezone_override'),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -68,7 +68,7 @@ class RegionalForm extends ConfigFormBase {
|
||||||
$system_date = $this->config('system.date');
|
$system_date = $this->config('system.date');
|
||||||
|
|
||||||
// Date settings:
|
// Date settings:
|
||||||
$zones = TimeZoneFormHelper::getOptionsList(FALSE, TRUE);
|
$zones = TimeZoneFormHelper::getOptionsListByRegion();
|
||||||
|
|
||||||
$form['locale'] = [
|
$form['locale'] = [
|
||||||
'#type' => 'details',
|
'#type' => 'details',
|
||||||
|
|
|
@ -1114,8 +1114,8 @@ function system_mail($key, &$message, $params) {
|
||||||
* @see https://www.drupal.org/node/3023528
|
* @see https://www.drupal.org/node/3023528
|
||||||
*/
|
*/
|
||||||
function system_time_zones($blank = NULL, $grouped = FALSE) {
|
function system_time_zones($blank = NULL, $grouped = FALSE) {
|
||||||
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. This function is no longer used in Drupal core. Use \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList() or \DateTimeZone::listIdentifiers() instead. See https://www.drupal.org/node/3023528', E_USER_DEPRECATED);
|
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. This function is no longer used in Drupal core. Use \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(), \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsListByRegion() or \DateTimeZone::listIdentifiers() instead. See https://www.drupal.org/node/3023528', E_USER_DEPRECATED);
|
||||||
return TimeZoneFormHelper::getOptionsList((bool) $blank, $grouped);
|
return $grouped ? TimeZoneFormHelper::getOptionsListByRegion((bool) $blank) : TimeZoneFormHelper::getOptionsList((bool) $blank);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -23,7 +23,7 @@ class SystemFunctionsLegacyTest extends KernelTestBase {
|
||||||
* @covers ::system_time_zones
|
* @covers ::system_time_zones
|
||||||
*/
|
*/
|
||||||
public function testSystemTimeZones() {
|
public function testSystemTimeZones() {
|
||||||
$this->expectDeprecation('system_time_zones() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. This function is no longer used in Drupal core. Use \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList() or \DateTimeZone::listIdentifiers() instead. See https://www.drupal.org/node/3023528');
|
$this->expectDeprecation('system_time_zones() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. This function is no longer used in Drupal core. Use \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsList(), \Drupal\Core\Datetime\TimeZoneFormHelper::getOptionsListByRegion() or \DateTimeZone::listIdentifiers() instead. See https://www.drupal.org/node/3023528');
|
||||||
system_time_zones();
|
system_time_zones();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -297,7 +297,7 @@ abstract class AccountForm extends ContentEntityForm implements TrustedCallbackI
|
||||||
'#type' => 'select',
|
'#type' => 'select',
|
||||||
'#title' => $this->t('Time zone'),
|
'#title' => $this->t('Time zone'),
|
||||||
'#default_value' => $account->getTimezone() ?: $system_date_config->get('timezone.default'),
|
'#default_value' => $account->getTimezone() ?: $system_date_config->get('timezone.default'),
|
||||||
'#options' => TimeZoneFormHelper::getOptionsList($account->id() != $user->id(), TRUE),
|
'#options' => TimeZoneFormHelper::getOptionsListByRegion($account->id() != $user->id()),
|
||||||
'#description' => $this->t('Select the desired local time and time zone. Dates and times throughout this site will be displayed using this time zone.'),
|
'#description' => $this->t('Select the desired local time and time zone. Dates and times throughout this site will be displayed using this time zone.'),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -120,7 +120,7 @@ class Date extends FieldPluginBase {
|
||||||
'#type' => 'select',
|
'#type' => 'select',
|
||||||
'#title' => $this->t('Timezone'),
|
'#title' => $this->t('Timezone'),
|
||||||
'#description' => $this->t('Timezone to be used for date output.'),
|
'#description' => $this->t('Timezone to be used for date output.'),
|
||||||
'#options' => ['' => $this->t('- Default site/user timezone -')] + TimeZoneFormHelper::getOptionsList(FALSE, TRUE),
|
'#options' => ['' => $this->t('- Default site/user timezone -')] + TimeZoneFormHelper::getOptionsListByRegion(),
|
||||||
'#default_value' => $this->options['timezone'],
|
'#default_value' => $this->options['timezone'],
|
||||||
];
|
];
|
||||||
foreach (array_merge(['custom'], array_keys($date_formats)) as $timezone_date_formats) {
|
foreach (array_merge(['custom'], array_keys($date_formats)) as $timezone_date_formats) {
|
||||||
|
|
|
@ -25,6 +25,7 @@ class TimeZoneFormHelperTest extends UnitTestCase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers ::getOptionsList
|
* @covers ::getOptionsList
|
||||||
|
* @covers ::getOptionsListByRegion
|
||||||
*/
|
*/
|
||||||
public function testGetList(): void {
|
public function testGetList(): void {
|
||||||
// Test the default parameters for getOptionsList().
|
// Test the default parameters for getOptionsList().
|
||||||
|
@ -33,8 +34,23 @@ class TimeZoneFormHelperTest extends UnitTestCase {
|
||||||
$this->assertArrayHasKey('Africa/Dar_es_Salaam', $result);
|
$this->assertArrayHasKey('Africa/Dar_es_Salaam', $result);
|
||||||
$this->assertEquals('Africa/Dar es Salaam', $result['Africa/Dar_es_Salaam']);
|
$this->assertEquals('Africa/Dar es Salaam', $result['Africa/Dar_es_Salaam']);
|
||||||
|
|
||||||
|
// Test that the ungrouped and grouped results have the same number of
|
||||||
|
// items.
|
||||||
|
$ungrouped_count = count(TimeZoneFormHelper::getOptionsList());
|
||||||
|
$grouped_result = TimeZoneFormHelper::getOptionsListByRegion();
|
||||||
|
$grouped_count = 0;
|
||||||
|
array_walk_recursive($grouped_result, function () use (&$grouped_count) {
|
||||||
|
$grouped_count++;
|
||||||
|
});
|
||||||
|
$this->assertEquals($ungrouped_count, $grouped_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::getOptionsListByRegion
|
||||||
|
*/
|
||||||
|
public function testGetGroupedList(): void {
|
||||||
// Tests time zone grouping.
|
// Tests time zone grouping.
|
||||||
$result = TimeZoneFormHelper::getOptionsList(FALSE, TRUE);
|
$result = TimeZoneFormHelper::getOptionsListByRegion();
|
||||||
|
|
||||||
// Check a two-level time zone.
|
// Check a two-level time zone.
|
||||||
$this->assertIsArray($result);
|
$this->assertIsArray($result);
|
||||||
|
@ -59,15 +75,6 @@ class TimeZoneFormHelperTest extends UnitTestCase {
|
||||||
// results array.
|
// results array.
|
||||||
$this->assertArrayNotHasKey('America/Indiana/Indianapolis', $result);
|
$this->assertArrayNotHasKey('America/Indiana/Indianapolis', $result);
|
||||||
|
|
||||||
// Test that the ungrouped and grouped results have the same number of
|
|
||||||
// items.
|
|
||||||
$ungrouped_count = count(TimeZoneFormHelper::getOptionsList());
|
|
||||||
$grouped_result = TimeZoneFormHelper::getOptionsList(FALSE, TRUE);
|
|
||||||
$grouped_count = 0;
|
|
||||||
array_walk_recursive($grouped_result, function () use (&$grouped_count) {
|
|
||||||
$grouped_count++;
|
|
||||||
});
|
|
||||||
$this->assertEquals($ungrouped_count, $grouped_count);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue