public function SmartDateTimezoneWidget::formatTimezoneOptions in Smart Date 3.4.x
Same name and namespace in other branches
- 3.0.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::formatTimezoneOptions()
- 3.1.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::formatTimezoneOptions()
- 3.2.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::formatTimezoneOptions()
- 3.3.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::formatTimezoneOptions()
Helper function to format allowed timezone as a grouped list.
1 call to SmartDateTimezoneWidget::formatTimezoneOptions()
- SmartDateTimezoneWidget::formElement in src/
Plugin/ Field/ FieldWidget/ SmartDateTimezoneWidget.php - Returns the form for a single field widget.
File
- src/
Plugin/ Field/ FieldWidget/ SmartDateTimezoneWidget.php, line 185
Class
- SmartDateTimezoneWidget
- Plugin implementation of the 'smartdate_timezone' widget.
Namespace
Drupal\smart_date\Plugin\Field\FieldWidgetCode
public function formatTimezoneOptions(array $zonelist, $grouped = TRUE) {
$prepend = '';
$append = '';
$add_abbr = $this
->getSetting('add_abbreviations');
$zones = [];
foreach ($zonelist as $zone) {
if (!is_string($zone)) {
$zone = $zone
->render();
}
// 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)),
]);
}
}
$now = time();
// 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 configured, add the timezone abbreviation.
if ($add_abbr) {
$tz = new \DateTimeZone(str_replace(' ', '_', $key));
$transition = $tz
->getTransitions($now, $now);
$abbr = $transition[0]['abbr'];
if ($add_abbr == 'before') {
$prepend = $abbr . ' ';
}
elseif ($add_abbr == 'after') {
$append = ' ' . $abbr;
}
}
if (!empty($region)) {
$label = empty($split) ? $city : $city . ' (' . implode('/', $split) . ')';
$grouped_zones[$region][$key] = $prepend . $label . $append;
}
else {
$grouped_zones[$key] = $prepend . $value . $append;
}
}
foreach ($grouped_zones as $key => $value) {
if (is_array($grouped_zones[$key])) {
asort($grouped_zones[$key]);
}
}
$zones = $grouped_zones;
}
return $zones;
}