Commit 2da1f424 authored by alexpott's avatar alexpott

Issue #2399211 by mpdonadio, legolasbo, vijaycs85, pjonckiere, rteijeiro,...

Issue #2399211 by mpdonadio, legolasbo, vijaycs85, pjonckiere, rteijeiro, jhodgdon, dawehner, yched: Support all options from views fields in DateTime formatters
parent 5217ef8c
......@@ -304,9 +304,33 @@ field.formatter.settings.uri_link:
type: mapping
label: 'URI as link display format settings'
field.formatter.settings.timestamp:
type: mapping
label: 'Timestamp display format settings'
mapping:
date_format:
type: string
label: 'Date format'
custom_date_format:
type: string
label: 'Custom date format'
timezone:
type: string
label: 'Time zone'
field.formatter.settings.timestamp_ago:
type: mapping
label: 'Timestamp ago display format settings'
mapping:
future_format:
type: string
label: 'Future format'
past_format:
type: string
label: 'Past format'
granularity:
type: integer
label: 'Granularity'
field.formatter.settings.entity_reference_entity_view:
type: mapping
......
......@@ -7,12 +7,15 @@
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Plugin implementation of the 'timestamp' formatter as time ago.
......@@ -36,6 +39,13 @@ class TimestampAgoFormatter extends FormatterBase implements ContainerFactoryPlu
*/
protected $dateFormatter;
/**
* The current Request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Constructs a TimestampAgoFormatter object.
*
......@@ -55,11 +65,14 @@ class TimestampAgoFormatter extends FormatterBase implements ContainerFactoryPlu
* Any third party settings.
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter service.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatter $date_formatter) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatter $date_formatter, Request $request) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->dateFormatter = $date_formatter;
$this->request = $request;
}
/**
......@@ -75,8 +88,66 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('date.formatter')
$container->get('date.formatter'),
$container->get('request_stack')->getCurrentRequest()
);
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'future_format' => '@interval hence',
'past_format' => '@interval ago',
'granularity' => 2,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$form['future_format'] = array(
'#type' => 'textfield',
'#title' => $this->t('Future format'),
'#default_value' => $this->getSetting('future_format'),
'#description' => $this->t('Use <em>@interval</em> where you want the formatted interval text to appear.'),
);
$form['past_format'] = array(
'#type' => 'textfield',
'#title' => $this->t('Past format'),
'#default_value' => $this->getSetting('past_format'),
'#description' => $this->t('Use <em>@interval</em> where you want the formatted interval text to appear.'),
);
$elements['granularity'] = array(
'#type' => 'number',
'#title' => $this->t('Granularity'),
'#description' => $this->t('How many time interval units should be shown in the formatted output.'),
'#default_value' => $this->getSetting('granularity') ?: 2,
'#min' => 1,
'#max' => 6,
);
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$future_date = strtotime('1 year 1 month 1 week 1 day 1 hour 1 minute');
$past_date = strtotime('-1 year -1 month -1 week -1 day -1 hour -1 minute');
$summary[] = $this->t('Future date: %display', array('%display' => $this->formatTimestamp($future_date)));
$summary[] = $this->t('Past date: %display', array('%display' => $this->formatTimestamp($past_date)));
return $summary;
}
/**
......@@ -87,7 +158,7 @@ public function viewElements(FieldItemListInterface $items) {
foreach ($items as $delta => $item) {
if ($item->value) {
$updated = $this->t('@time ago', array('@time' => $this->dateFormatter->formatTimeDiffSince($item->value)));
$updated = $this->formatTimestamp($item->value);
}
else {
$updated = $this->t('never');
......@@ -99,4 +170,25 @@ public function viewElements(FieldItemListInterface $items) {
return $elements;
}
/**
* Formats a timestamp.
*
* @param int $timestamp
* A UNIX timestamp to format.
*
* @return string
* The formatted timestamp string using the past or future format setting.
*/
protected function formatTimestamp($timestamp) {
$granularity = $this->getSetting('granularity');
$options = ['granularity' => $granularity];
if ($this->request->server->get('REQUEST_TIME') > $timestamp) {
return SafeMarkup::format($this->getSetting('past_format'), ['@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, $options)]);
}
else {
return SafeMarkup::format($this->getSetting('future_format'), ['@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, $options)]);
}
}
}
......@@ -7,8 +7,14 @@
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Datetime\DateFormatter;
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\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'timestamp' formatter.
......@@ -23,7 +29,138 @@
* }
* )
*/
class TimestampFormatter extends FormatterBase {
class TimestampFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected $dateFormatter;
/**
* The date format entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $dateFormatStorage;
/**
* Constructs a new TimestampFormatter.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Third party settings.
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter service.
* @param \Drupal\Core\Entity\EntityStorageInterface $date_format_storage
* The date format storage.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatter $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;
}
/**
* {@inheritdoc}
*/
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.manager')->getStorage('date_format')
);
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'date_format' => 'medium',
'custom_date_format' => '',
'timezone' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$date_formats = array();
foreach ($this->dateFormatStorage->loadMultiple() as $machine_name => $value) {
$date_formats[$machine_name] = $this->t('@name format: @date', array('@name' => $value->label(), '@date' => $this->dateFormatter->format(REQUEST_TIME, $machine_name)));
}
$date_formats['custom'] = $this->t('Custom');
$elements['date_format'] = array(
'#type' => 'select',
'#title' => $this->t('Date format'),
'#options' => $date_formats,
'#default_value' => $this->getSetting('date_format') ?: 'medium',
);
$elements['custom_date_format'] = array(
'#type' => 'textfield',
'#title' => $this->t('Custom date format'),
'#description' => $this->t('See <a href="@url" target="_blank">the documentation for PHP date formats</a>.', ['@url' => 'http://php.net/manual/function.date.php']),
'#default_value' => $this->getSetting('custom_date_format') ?: '',
);
$elements['custom_date_format']['#states']['visible'][] = array(
':input[name="options[settings][date_format]"]' => array('value' => 'custom'),
);
$elements['timezone'] = array(
'#type' => 'select',
'#title' => $this->t('Time zone'),
'#options' => array('' => $this->t('- Default site/user time zone -')) + system_time_zones(FALSE),
'#default_value' => $this->getSetting('timezone'),
);
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$date_format = $this->getSetting('date_format');
$summary[] = $this->t('Date format: @date_format', array('@date_format' => $date_format));
if ($this->getSetting('date_format') === 'custom' && ($custom_date_format = $this->getSetting('custom_date_format'))) {
$summary[] = $this->t('Custom date format: @custom_date_format', array('@custom_date_format' => $custom_date_format));
}
if ($timezone = $this->getSetting('timezone')) {
$summary[] = $this->t('Time zone: @timezone', array('@timezone' => $timezone));
}
return $summary;
}
/**
* {@inheritdoc}
......@@ -31,6 +168,17 @@ class TimestampFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items) {
$elements = array();
$date_format = $this->getSetting('date_format');
$custom_date_format = '';
$timezone = $this->getSetting('timezone') ?: NULL;
$langcode = NULL;
// If an RFC2822 date format is requested, then the month and day have to
// be in English. @see http://www.faqs.org/rfcs/rfc2822.html
if ($date_format === 'custom' && ($custom_date_format = $this->getSetting('custom_date_format')) === 'r') {
$langcode = 'en';
}
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#cache' => [
......@@ -38,7 +186,7 @@ public function viewElements(FieldItemListInterface $items) {
'timezone',
],
],
'#markup' => format_date($item->value)
'#markup' => $this->dateFormatter->format($item->value, $date_format, $custom_date_format, $timezone, $langcode),
];
}
......
......@@ -23,8 +23,15 @@ field.value.datetime:
type: string
label: 'Default date value'
field.formatter.settings.datetime_default:
field.formatter.settings.datetime_base:
type: mapping
mapping:
timezone_override:
type: string
label: 'Time zone override'
field.formatter.settings.datetime_default:
type: field.formatter.settings.datetime_base
label: 'Datetime default display format settings'
mapping:
format_type:
......@@ -32,9 +39,33 @@ field.formatter.settings.datetime_default:
label: 'Date format'
field.formatter.settings.datetime_plain:
type: mapping
type: field.formatter.settings.datetime_base
label: 'Datetime plain display format settings'
field.formatter.settings.datetime_custom:
type: field.formatter.settings.datetime_base
label: 'Datetime custom display format settings'
mapping:
date_format:
type: string
label: 'Date/time format'
translatable: true
translation context: 'PHP date format'
field.formatter.settings.datetime_time_ago:
type: mapping
label: 'Datetime time ago display format settings'
mapping:
future_format:
type: string
label: 'Future format'
past_format:
type: string
label: 'Past format'
granularity:
type: integer
label: 'Granularity'
field.widget.settings.datetime_datelist:
type: mapping
label: 'Datetime select list display format settings'
......
<?php
/**
* @file
* Contains \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeCustomFormatter.
*/
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'Custom' formatter for 'datetime' fields.
*
* @FieldFormatter(
* id = "datetime_custom",
* label = @Translation("Custom"),
* field_types = {
* "datetime"
* }
*)
*/
class DateTimeCustomFormatter extends DateTimeFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'date_format' => DATETIME_DATETIME_STORAGE_FORMAT,
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
foreach ($items as $delta => $item) {
$output = '';
if (!empty($item->date)) {
/** @var \Drupal\Core\Datetime\DrupalDateTime $date */
$date = $item->date;
if ($this->getFieldSetting('datetime_type') == 'date') {
// A date without time will pick up the current time, use the default.
datetime_date_default_time($date);
}
$this->setTimeZone($date);
$output = $this->formatDate($date);
}
$elements[$delta] = [
'#markup' => $output,
'#cache' => [
'contexts' => [
'timezone',
],
],
];
}
return $elements;
}
/**
* {@inheritdoc}
*/
protected function formatDate($date) {
$format = $this->getSetting('date_format');
$timezone = $this->getSetting('timezone_override');
return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['date_format'] = array(
'#type' => 'textfield',
'#title' => $this->t('Date/time format'),
'#description' => $this->t('See <a href="@url" target="_blank">the documentation for PHP date formats</a>.', ['@url' => 'http://php.net/manual/function.date.php']),
'#default_value' => $this->getSetting('date_format'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$date = new DrupalDateTime();
$this->setTimeZone($date);
$summary[] = $date->format($this->getSetting('date_format'), $this->getFormatSettings());
return $summary;
}
}
......@@ -7,18 +7,12 @@
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Field\FormatterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'datetime_default' formatter.
* Plugin implementation of the 'Default' formatter for 'datetime' fields.
*
* @FieldFormatter(
* id = "datetime_default",
......@@ -28,7 +22,7 @@
* }
* )
*/
class DateTimeDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
class DateTimeDefaultFormatter extends DateTimeFormatterBase {
/**
* {@inheritdoc}
......@@ -39,90 +33,29 @@ public static function defaultSettings() {
) + parent::defaultSettings();
}
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected $dateFormatter;
/**
* The date storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $dateStorage;
/**
* Constructs a new DateTimeDefaultFormatter.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Third party settings.
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter service.
* @param \Drupal\Core\Entity\EntityStorageInterface $date_storage
* The date storage.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatter $date_formatter, EntityStorageInterface $date_storage) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->dateFormatter = $date_formatter;
$this->dateStorage = $date_storage;
}
/**
* {@inheritdoc}
*/
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.manager')->getStorage('date_format')
);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items) {
$elements = array();
foreach ($items as $delta => $item) {
$formatted_date = '';
$output = '';
$iso_date = '';
if ($item->date) {
/** @var \Drupal\Core\Datetime\DrupalDateTime $date */
$date = $item->date;
// Create the ISO date in Universal Time.
$iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
// The formatted output will be in local time.
$date->setTimeZone(timezone_open(drupal_get_user_timezone()));
if ($this->getFieldSetting('datetime_type') == 'date') {
// A date without time will pick up the current time, use the default.
datetime_date_default_time($date);
}
$formatted_date = $this->dateFormat($date);
$this->setTimeZone($date);
$output = $this->formatDate($date);
}
// Display the date using theme datetime.
......@@ -133,7 +66,8 @@ public function viewElements(FieldItemListInterface $items) {
],
],
'#theme' => 'time',
'#text' => $formatted_date,
'#text' => $output,
'#html' => FALSE,
'#attributes' => array(
'datetime' => $iso_date,
),
......@@ -151,31 +85,29 @@ public function viewElements(FieldItemListInterface $items) {
}
/**
* Creates a formatted date value as a string.
*
* @param object $date
* A date object.
*
* @return string
* A formatted date string using the chosen format.
* {@inheritdoc}
*/
function dateFormat($date) {
protected function formatDate($date) {
$format_type = $this->getSetting('format_type');
return $this->dateFormatter->format($date->getTimestamp(), $format_type);
$timezone = $this->getSetting('timezone_override');
return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$time = new DrupalDateTime();
$format_types = $this->dateStorage->loadMultiple();
$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 . ')';