View source
<?php
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class DateTimeDefaultFormatter extends DateTimeFormatterBase {
public static function defaultSettings() {
return array(
'format_type' => 'medium',
) + parent::defaultSettings();
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($items as $delta => $item) {
$output = '';
$iso_date = '';
if ($item->date) {
$date = $item->date;
$iso_date = $date
->format("Y-m-d\\TH:i:s") . 'Z';
if ($this
->getFieldSetting('datetime_type') == 'date') {
datetime_date_default_time($date);
}
$this
->setTimeZone($date);
$output = $this
->formatDate($date);
}
$elements[$delta] = array(
'#cache' => [
'contexts' => [
'timezone',
],
],
'#theme' => 'time',
'#text' => $output,
'#html' => FALSE,
'#attributes' => array(
'datetime' => $iso_date,
),
);
if (!empty($item->_attributes)) {
$elements[$delta]['#attributes'] += $item->_attributes;
unset($item->_attributes);
}
}
return $elements;
}
protected function formatDate($date) {
$format_type = $this
->getSetting('format_type');
$timezone = $this
->getSetting('timezone_override');
return $this->dateFormatter
->format($date
->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL);
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$time = new DrupalDateTime();
$format_types = $this->dateFormatStorage
->loadMultiple();
$options = [];
foreach ($format_types as $type => $type_info) {
$format = $this->dateFormatter
->format($time
->format('U'), $type);
$options[$type] = $type_info
->label() . ' (' . $format . ')';
}
$form['format_type'] = array(
'#type' => 'select',
'#title' => t('Date format'),
'#description' => t("Choose a format for displaying the date. Be sure to set a format appropriate for the field, i.e. omitting time for a field that only has a date."),
'#options' => $options,
'#default_value' => $this
->getSetting('format_type'),
);
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$date = new DrupalDateTime();
$summary[] = t('Format: @display', array(
'@display' => $this
->formatDate($date, $this
->getFormatSettings()),
));
return $summary;
}
}