Issue #3020455 by Spokje, jhedstrom, kim.pepper, Bhanu951, mpdonadio, alexpott, smustgrave: Deprecate system_time_zones() and move to TimeZoneFormHelper
parent
8d2e488062
commit
efda42f2fc
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Core\Datetime;
|
||||
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
/**
|
||||
* Helper class for dealing with timezones.
|
||||
*/
|
||||
class TimeZoneFormHelper {
|
||||
|
||||
/**
|
||||
* Generate an array of time zones names.
|
||||
*
|
||||
* 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.
|
||||
* @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 {
|
||||
$zonelist = \DateTimeZone::listIdentifiers();
|
||||
$zones = $blank ? ['' => new TranslatableMarkup('- None selected -')] : [];
|
||||
foreach ($zonelist as $zone) {
|
||||
$zones[$zone] = new TranslatableMarkup(str_replace('_', ' ', $zone));
|
||||
}
|
||||
// Sort the translated time zones alphabetically.
|
||||
asort($zones);
|
||||
if ($grouped) {
|
||||
$grouped_zones = [];
|
||||
foreach ($zones as $key => $value) {
|
||||
$split = explode('/', $value);
|
||||
$city = array_pop($split);
|
||||
$region = array_shift($split);
|
||||
if (!empty($region)) {
|
||||
$grouped_zones[$region][$key] = empty($split) ? $city : $city . ' (' . implode('/', $split) . ')';
|
||||
}
|
||||
else {
|
||||
$grouped_zones[$key] = $value;
|
||||
}
|
||||
}
|
||||
foreach ($grouped_zones as $key => $value) {
|
||||
if (is_array($grouped_zones[$key])) {
|
||||
asort($grouped_zones[$key]);
|
||||
}
|
||||
}
|
||||
$zones = $grouped_zones;
|
||||
}
|
||||
|
||||
return $zones;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Datetime\DateFormatterInterface;
|
||||
use Drupal\Core\Datetime\TimeZoneFormHelper;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
|
@ -131,7 +132,7 @@ class TimestampFormatter extends FormatterBase {
|
|||
$elements['timezone'] = [
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Time zone'),
|
||||
'#options' => ['' => $this->t('- Default site/user time zone -')] + system_time_zones(FALSE, TRUE),
|
||||
'#options' => ['' => $this->t('- Default site/user time zone -')] + TimeZoneFormHelper::getOptionsList(FALSE, TRUE),
|
||||
'#default_value' => $this->getSetting('timezone'),
|
||||
];
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Drupal\Core\Installer\Form;
|
||||
|
||||
use Drupal\Core\Datetime\TimeZoneFormHelper;
|
||||
use Drupal\Core\Extension\ModuleInstallerInterface;
|
||||
use Drupal\Core\Form\ConfigFormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
@ -207,7 +208,7 @@ class SiteConfigureForm extends ConfigFormBase {
|
|||
'#type' => 'select',
|
||||
'#title' => $this->t('Default time zone'),
|
||||
'#default_value' => $default_timezone,
|
||||
'#options' => system_time_zones(NULL, TRUE),
|
||||
'#options' => TimeZoneFormHelper::getOptionsList(FALSE, TRUE),
|
||||
'#weight' => 5,
|
||||
'#attributes' => ['class' => ['timezone-detect']],
|
||||
'#access' => empty($install_state['config_install_path']),
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Drupal\datetime\Plugin\Field\FieldFormatter;
|
|||
|
||||
use Drupal\Core\Datetime\DateFormatterInterface;
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\Datetime\TimeZoneFormHelper;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
|
@ -97,7 +98,7 @@ abstract class DateTimeFormatterBase extends FormatterBase {
|
|||
'#type' => 'select',
|
||||
'#title' => $this->t('Time zone override'),
|
||||
'#description' => $this->t('The time zone selected here will always be used'),
|
||||
'#options' => system_time_zones(TRUE, TRUE),
|
||||
'#options' => TimeZoneFormHelper::getOptionsList(TRUE, TRUE),
|
||||
'#default_value' => $this->getSetting('timezone_override'),
|
||||
];
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\system\Form;
|
||||
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Datetime\TimeZoneFormHelper;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Locale\CountryManagerInterface;
|
||||
use Drupal\Core\Form\ConfigFormBase;
|
||||
|
@ -67,7 +68,7 @@ class RegionalForm extends ConfigFormBase {
|
|||
$system_date = $this->config('system.date');
|
||||
|
||||
// Date settings:
|
||||
$zones = system_time_zones(NULL, TRUE);
|
||||
$zones = TimeZoneFormHelper::getOptionsList(FALSE, TRUE);
|
||||
|
||||
$form['locale'] = [
|
||||
'#type' => 'details',
|
||||
|
|
|
@ -13,6 +13,7 @@ use Drupal\Core\Asset\AttachedAssetsInterface;
|
|||
use Drupal\Core\Block\BlockPluginInterface;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Database\Query\AlterableInterface;
|
||||
use Drupal\Core\Datetime\TimeZoneFormHelper;
|
||||
use Drupal\Core\Entity\ContentEntityTypeInterface;
|
||||
use Drupal\Core\Extension\Extension;
|
||||
use Drupal\Core\File\Exception\FileException;
|
||||
|
@ -1102,42 +1103,17 @@ function system_mail($key, &$message, $params) {
|
|||
*
|
||||
* @return array
|
||||
* An array or nested array containing time zones, keyed by the system name.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
function system_time_zones($blank = NULL, $grouped = FALSE) {
|
||||
$zonelist = timezone_identifiers_list();
|
||||
$zones = $blank ? ['' => t('- None selected -')] : [];
|
||||
foreach ($zonelist as $zone) {
|
||||
// Because many time zones exist in PHP only for backward compatibility
|
||||
// reasons and should not be used, the list is filtered by a regular
|
||||
// expression.
|
||||
if (preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) {
|
||||
$zones[$zone] = t('@zone', ['@zone' => t(str_replace('_', ' ', $zone))]);
|
||||
}
|
||||
}
|
||||
// Sort the translated time zones alphabetically.
|
||||
asort($zones);
|
||||
if ($grouped) {
|
||||
$grouped_zones = [];
|
||||
foreach ($zones as $key => $value) {
|
||||
$split = explode('/', $value);
|
||||
$city = array_pop($split);
|
||||
$region = array_shift($split);
|
||||
if (!empty($region)) {
|
||||
$grouped_zones[$region][$key] = empty($split) ? $city : $city . ' (' . implode('/', $split) . ')';
|
||||
}
|
||||
else {
|
||||
$grouped_zones[$key] = $value;
|
||||
}
|
||||
}
|
||||
foreach ($grouped_zones as $key => $value) {
|
||||
if (is_array($grouped_zones[$key])) {
|
||||
asort($grouped_zones[$key]);
|
||||
}
|
||||
}
|
||||
$zones = $grouped_zones;
|
||||
}
|
||||
|
||||
return $zones;
|
||||
@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);
|
||||
return TimeZoneFormHelper::getOptionsList((bool) $blank, $grouped);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\system\Kernel\System;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests the deprecations in the system module.
|
||||
*
|
||||
* @group system
|
||||
* @group legacy
|
||||
*/
|
||||
class SystemFunctionsLegacyTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = [
|
||||
'system',
|
||||
];
|
||||
|
||||
/**
|
||||
* @covers ::system_time_zones
|
||||
*/
|
||||
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');
|
||||
system_time_zones();
|
||||
}
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\user;
|
||||
|
||||
use Drupal\Component\Datetime\TimeInterface;
|
||||
use Drupal\Core\Datetime\TimeZoneFormHelper;
|
||||
use Drupal\Core\Entity\ContentEntityForm;
|
||||
use Drupal\Core\Entity\EntityConstraintViolationListInterface;
|
||||
use Drupal\Core\Entity\EntityRepositoryInterface;
|
||||
|
@ -296,7 +297,7 @@ abstract class AccountForm extends ContentEntityForm implements TrustedCallbackI
|
|||
'#type' => 'select',
|
||||
'#title' => $this->t('Time zone'),
|
||||
'#default_value' => $account->getTimezone() ?: $system_date_config->get('timezone.default'),
|
||||
'#options' => system_time_zones($account->id() != $user->id(), TRUE),
|
||||
'#options' => TimeZoneFormHelper::getOptionsList($account->id() != $user->id(), TRUE),
|
||||
'#description' => $this->t('Select the desired local time and time zone. Dates and times throughout this site will be displayed using this time zone.'),
|
||||
];
|
||||
|
||||
|
|
|
@ -572,7 +572,7 @@ class User extends ContentEntityBase implements UserInterface {
|
|||
* The allowed values.
|
||||
*/
|
||||
public static function getAllowedTimezones() {
|
||||
return array_keys(system_time_zones());
|
||||
return \DateTimeZone::listIdentifiers();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Drupal\user\Plugin\migrate\process\d6;
|
||||
|
||||
use Drupal\Core\Datetime\TimeZoneFormHelper;
|
||||
use Drupal\migrate\MigrateExecutableInterface;
|
||||
use Drupal\migrate\ProcessPluginBase;
|
||||
use Drupal\migrate\Row;
|
||||
|
@ -39,7 +40,7 @@ class UserUpdate7002 extends ProcessPluginBase implements ContainerFactoryPlugin
|
|||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->dateConfig = $date_config;
|
||||
if (!isset(static::$timezones)) {
|
||||
static::$timezones = system_time_zones();
|
||||
static::$timezones = TimeZoneFormHelper::getOptionsList();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Drupal\views\Plugin\views\field;
|
||||
|
||||
use Drupal\Core\Datetime\TimeZoneFormHelper;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\views\ResultRow;
|
||||
|
@ -119,7 +120,7 @@ class Date extends FieldPluginBase {
|
|||
'#type' => 'select',
|
||||
'#title' => $this->t('Timezone'),
|
||||
'#description' => $this->t('Timezone to be used for date output.'),
|
||||
'#options' => ['' => $this->t('- Default site/user timezone -')] + system_time_zones(FALSE, TRUE),
|
||||
'#options' => ['' => $this->t('- Default site/user timezone -')] + TimeZoneFormHelper::getOptionsList(FALSE, TRUE),
|
||||
'#default_value' => $this->options['timezone'],
|
||||
];
|
||||
foreach (array_merge(['custom'], array_keys($date_formats)) as $timezone_date_formats) {
|
||||
|
|
|
@ -1,30 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\system\Kernel\Timezone;
|
||||
namespace Drupal\Tests\Core\Datetime;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\Core\Datetime\TimeZoneFormHelper;
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* Test coverage for time zone handling.
|
||||
*
|
||||
* @group system
|
||||
* @coversDefaultClass \Drupal\Core\Datetime\TimeZoneFormHelper
|
||||
* @group Datetime
|
||||
*/
|
||||
class TimezoneTest extends KernelTestBase {
|
||||
|
||||
protected static $modules = ['system'];
|
||||
class TimeZoneFormHelperTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* Tests system_time_zones().
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testSystemTimeZones() {
|
||||
// Test the default parameters for system_time_zones().
|
||||
$result = system_time_zones();
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->set('string_translation', $this->getStringTranslationStub());
|
||||
\Drupal::setContainer($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getOptionsList
|
||||
*/
|
||||
public function testGetList(): void {
|
||||
// Test the default parameters for getOptionsList().
|
||||
$result = TimeZoneFormHelper::getOptionsList();
|
||||
$this->assertIsArray($result);
|
||||
$this->assertArrayHasKey('Africa/Dar_es_Salaam', $result);
|
||||
$this->assertEquals('Africa/Dar es Salaam', $result['Africa/Dar_es_Salaam']);
|
||||
|
||||
// Tests time zone grouping.
|
||||
$result = system_time_zones(NULL, TRUE);
|
||||
$result = TimeZoneFormHelper::getOptionsList(FALSE, TRUE);
|
||||
|
||||
// Check a two-level time zone.
|
||||
$this->assertIsArray($result);
|
||||
|
@ -51,8 +61,8 @@ class TimezoneTest extends KernelTestBase {
|
|||
|
||||
// Test that the ungrouped and grouped results have the same number of
|
||||
// items.
|
||||
$ungrouped_count = count(system_time_zones());
|
||||
$grouped_result = system_time_zones(NULL, TRUE);
|
||||
$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++;
|
Loading…
Reference in New Issue