You are here

public function SmartDateTimezoneWidget::formatTimezoneOptions in Smart Date 3.0.x

Same name and namespace in other branches
  1. 3.1.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::formatTimezoneOptions()
  2. 3.2.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::formatTimezoneOptions()
  3. 3.3.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::formatTimezoneOptions()
  4. 3.4.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 164

Class

SmartDateTimezoneWidget
Plugin implementation of the 'smartdate_timezone' widget.

Namespace

Drupal\smart_date\Plugin\Field\FieldWidget

Code

public function formatTimezoneOptions(array $zonelist, $grouped = TRUE) {
  $zones = [];
  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;
}