View source
<?php
namespace Drupal\smart_date\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
class SmartDateTimezoneWidget extends SmartDateDefaultWidget implements ContainerFactoryPluginInterface {
public static function defaultSettings() {
return [
'default_tz' => '',
'custom_tz' => '',
'allowed_timezones' => [],
] + parent::defaultSettings();
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$default_label = t('- default: @tz_label -', [
'@tz_label' => $this
->getSiteTimezone(),
]);
switch ($this
->getSetting('default_tz')) {
case '':
$default_timezone = '';
break;
case 'user':
$default_timezone = date_default_timezone_get();
break;
case 'custom':
$default_timezone = $this
->getSetting('custom_tz');
break;
}
$timezones = $this
->getSetting('allowed_timezones') ? $this
->formatTimezoneOptions($this
->getSetting('allowed_timezones')) : $this
->getTimezones();
$element['timezone']['#type'] = 'select';
$element['timezone']['#options'] = [
'' => $default_label,
] + $timezones;
$element['timezone']['#default_value'] = $items[$delta]->timezone ? $items[$delta]->timezone : $default_timezone;
$element['timezone']['#attributes']['class'][] = 'field-timezone';
$element['timezone']['#weight'] = 100;
return $element;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['default_tz'] = [
'#type' => 'select',
'#title' => $this
->t('Default timezone'),
'#default_value' => $this
->getSetting('default_tz'),
'#options' => [
'' => $this
->t('Site default (ignores any user override)'),
'user' => $this
->t("User's timezone, defaulting to site (always saved)"),
'custom' => $this
->t('A custom timezone (always saved)'),
],
];
$custom_tz = $this
->getSetting('custom_tz') ? $this
->getSetting('custom_tz') : $this
->getSiteTimezone();
$element['custom_tz'] = [
'#type' => 'select',
'#title' => $this
->t('Custom timezone'),
'#default_value' => $custom_tz,
'#options' => $this
->getTimezones(),
'#states' => [
'visible' => [
'select[name$="[settings][default_tz]"]' => [
'value' => 'custom',
],
],
],
];
$default_label = t('- default: @tz_label -', [
'@tz_label' => $this
->getSiteTimezone(),
]);
switch ($this
->getSetting('default_tz')) {
case '':
$default_timezone = '';
break;
case 'user':
$default_timezone = date_default_timezone_get();
break;
case 'custom':
$default_timezone = $this
->getSetting('custom_tz');
break;
}
$element['allowed_timezones']['#type'] = 'select';
$element['allowed_timezones']['#multiple'] = TRUE;
$element['allowed_timezones']['#options'] = $this
->getTimezones();
$element['allowed_timezones']['#default_value'] = $this
->getSetting('allowed_timezones');
$element['allowed_timezones']['#weight'] = 100;
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
switch ($this
->getSetting('default_tz')) {
case '':
$summary[] = $this
->t("The site's timezone will be used unless overridden");
break;
case 'user':
$summary[] = $this
->t("The user's timezone will be used by default");
break;
case 'custom':
$summary[] = $this
->t('Custom default timezone: @custom_tz', [
'@custom_tz' => $this
->getSetting('custom_tz'),
]);
break;
}
if ($allowed_tz = $this
->getSetting('allowed_timezones')) {
$summary[] = $this
->t('Allowed timezones: @timezones', [
'@timezones' => implode(', ', $allowed_tz),
]);
}
return $summary;
}
public function getTimezones() {
return system_time_zones(FALSE, TRUE);
}
public function formatTimezoneOptions(array $zonelist, $grouped = TRUE) {
$zones = [];
foreach ($zonelist as $zone) {
if (preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) {
$zones[$zone] = t('@zone', [
'@zone' => t(str_replace('_', ' ', $zone)),
]);
}
}
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;
}
public function getSiteTimezone() {
$config = \Drupal::config('system.date');
$config_data_default_timezone = $config
->get('timezone.default');
return !empty($config_data_default_timezone) ? $config_data_default_timezone : @date_default_timezone_get();
}
}