View source
<?php
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class DateTimeFormatterBase extends FormatterBase {
protected $dateFormatter;
protected $dateFormatStorage;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatterInterface $date_formatter, EntityStorageInterface $date_format_storage) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->dateFormatter = $date_formatter;
$this->dateFormatStorage = $date_format_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('date.formatter'), $container
->get('entity_type.manager')
->getStorage('date_format'));
}
public static function defaultSettings() {
return [
'timezone_override' => '',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['timezone_override'] = [
'#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),
'#default_value' => $this
->getSetting('timezone_override'),
];
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
if ($override = $this
->getSetting('timezone_override')) {
$summary[] = $this
->t('Time zone: @timezone', [
'@timezone' => $override,
]);
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
if ($item->date) {
$date = $item->date;
$elements[$delta] = $this
->buildDateWithIsoAttribute($date);
if (!empty($item->_attributes)) {
$elements[$delta]['#attributes'] += $item->_attributes;
unset($item->_attributes);
}
}
}
return $elements;
}
protected abstract function formatDate($date);
protected function setTimeZone(DrupalDateTime $date) {
if ($this
->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
$timezone = DateTimeItemInterface::STORAGE_TIMEZONE;
}
else {
$timezone = date_default_timezone_get();
}
$date
->setTimeZone(timezone_open($timezone));
}
protected function getFormatSettings() {
$settings = [];
if ($this
->getSetting('timezone_override') != '') {
$settings['timezone'] = $this
->getSetting('timezone_override');
}
return $settings;
}
protected function buildDate(DrupalDateTime $date) {
$this
->setTimeZone($date);
$build = [
'#markup' => $this
->formatDate($date),
'#cache' => [
'contexts' => [
'timezone',
],
],
];
return $build;
}
protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
$iso_date = $date
->format("Y-m-d\\TH:i:s") . 'Z';
$this
->setTimeZone($date);
$build = [
'#theme' => 'time',
'#text' => $this
->formatDate($date),
'#attributes' => [
'datetime' => $iso_date,
],
'#cache' => [
'contexts' => [
'timezone',
],
],
];
return $build;
}
}