diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampAgoFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampAgoFormatter.php
index 68d29ba..ea99742 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampAgoFormatter.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampAgoFormatter.php
@@ -98,8 +98,8 @@ public static function create(ContainerInterface $container, array $configuratio
*/
public static function defaultSettings() {
return array(
- 'future_format' => '@time hence',
- 'past_format' => '@time ago',
+ 'future_format' => '@interval hence',
+ 'past_format' => '@interval ago',
'granularity' => 2,
) + parent::defaultSettings();
}
@@ -114,14 +114,14 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#title' => $this->t('Future format'),
'#default_value' => $this->getSetting('future_format'),
- '#description' => $this->t('The format for time intervals in the future. Use @interval where you want the time to appear.'),
+ '#description' => $this->t('Use @interval 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('The format for time intervals in the past. Use @interval where you want the time to appear.'),
+ '#description' => $this->t('Use @interval where you want the formatted interval text to appear.'),
);
$elements['granularity'] = array(
@@ -171,14 +171,14 @@ public function viewElements(FieldItemListInterface $items) {
}
/**
- * Creates a formatted timestamp value as a string.
- *
- * @param int $timestamp
- * A UNIX timestamp to format.
- *
- * @return string
- * The formatted timestamp string using the past or future format setting.
- */
+ * 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];
diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php
index 185ee7f..855b880 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php
@@ -65,7 +65,7 @@ class TimestampFormatter extends FormatterBase implements ContainerFactoryPlugin
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter service.
* @param \Drupal\Core\Entity\EntityStorageInterface $date_format_storage
- * The date 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);
@@ -126,7 +126,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['custom_date_format'] = array(
'#type' => 'textfield',
'#title' => $this->t('Custom date format'),
- '#description' => $this->t('If "Custom", see the documentation for PHP date formats.'),
+ '#description' => $this->t('Used if Date format is set to Custom. See the documentation for PHP date formats.'),
'#default_value' => $this->getSetting('custom_date_format') ?: '',
);
@@ -137,7 +137,6 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['timezone'] = array(
'#type' => 'select',
'#title' => $this->t('Time zone'),
- '#description' => $this->t('Time zone to be used for date output.'),
'#options' => array('' => $this->t('- Default site/user time zone -')) + system_time_zones(FALSE),
'#default_value' => $this->getSetting('timezone'),
);
diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php
index 39b3b4f..248c0bd 100644
--- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php
+++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php
@@ -51,7 +51,7 @@ public function viewElements(FieldItemListInterface $items) {
}
$this->setTimeZone($date);
- $output = $date->format($this->getSetting('date_format'), $this->getFormatSettings());
+ $output = $this->formatDate($date);
}
$elements[$delta] = [
'#markup' => $output,
@@ -69,13 +69,22 @@ public function viewElements(FieldItemListInterface $items) {
/**
* {@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('A user-defined date/time format. See the documentation for PHP date formats.'),
+ '#description' => $this->t('See the documentation for PHP date formats.'),
'#default_value' => $this->getSetting('date_format'),
);
diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php
index 8e85e88..588f69f 100644
--- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php
+++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php
@@ -7,14 +7,9 @@
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 Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'Default' formatter for 'datetime' fields.
@@ -27,7 +22,7 @@
* }
* )
*/
-class DateTimeDefaultFormatter extends DateTimeFormatterBase implements ContainerFactoryPluginInterface {
+class DateTimeDefaultFormatter extends DateTimeFormatterBase {
/**
* {@inheritdoc}
@@ -39,66 +34,6 @@ public static function defaultSettings() {
}
/**
- * 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 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_format_storage
- * The date format entity 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 function viewElements(FieldItemListInterface $items) {
@@ -120,7 +55,7 @@ public function viewElements(FieldItemListInterface $items) {
}
$this->setTimeZone($date);
- $output = $this->formatDate($date, $this->getFormatSettings());
+ $output = $this->formatDate($date);
}
// Display the date using theme datetime.
@@ -150,18 +85,12 @@ 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}
*/
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);
+ return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL);
}
/**
diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php
index 97e8b75..aad8c16 100644
--- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php
+++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php
@@ -7,14 +7,80 @@
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\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
/**
* Base class for 'DateTime Field formatter' plugin implementations.
*/
-abstract class DateTimeFormatterBase extends FormatterBase {
+abstract class DateTimeFormatterBase 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 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_format_storage
+ * The date format entity 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}
@@ -56,7 +122,24 @@ public function settingsSummary() {
}
/**
- * Sets the current user's or overridden time zone on a DrupalDateTime object.
+ * Creates a formatted date value as a string.
+ *
+ * @param object $date
+ * A date object.
+ *
+ * @return string
+ * A formatted date string using the chosen format.
+ */
+ abstract protected function formatDate($date);
+
+ /**
+ * Sets the proper time zone on a DrupalDateTime object for the current user.
+ *
+ * A DrupalDateTime object loaded from the database will have the UTC time
+ * zone applied to it. This method will apply the time zone for the current
+ * user, based on system and user settings.
+ *
+ * @see drupal_get_user_timezone()
*
* @param \Drupal\Core\Datetime\DrupalDateTime $date
* A DrupalDateTime object.
diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php
index 79e3270..b92c07c 100644
--- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php
+++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php
@@ -37,14 +37,12 @@ public function viewElements(FieldItemListInterface $items) {
if ($this->getFieldSetting('datetime_type') == 'date') {
// A date without time will pick up the current time, use the default.
datetime_date_default_time($date);
- $format = DATETIME_DATE_STORAGE_FORMAT;
}
else {
- $format = DATETIME_DATETIME_STORAGE_FORMAT;
}
$this->setTimeZone($date);
- $output = $date->format($format, $this->getFormatSettings());
+ $output = $this->formatDate($date);
}
$elements[$delta] = [
'#cache' => [
@@ -59,4 +57,13 @@ public function viewElements(FieldItemListInterface $items) {
return $elements;
}
+ /**
+ * {@inheritdoc}
+ */
+ protected function formatDate($date) {
+ $format = $this->getFieldSetting('datetime_type') == 'date' ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
+ $timezone = $this->getSetting('timezone_override');
+ return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
+ }
+
}
diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php
index 3f17fe3..6db7cf8 100644
--- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php
+++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php
@@ -136,19 +136,19 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#title' => $this->t('Future format'),
'#default_value' => $this->getSetting('future_format'),
- '#description' => $this->t('The format for time intervals in the future. Use @interval where you want the time to appear.'),
+ '#description' => $this->t('Use @interval 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('The format for time intervals in the past. Use @interval where you want the time to appear.'),
+ '#description' => $this->t('Use @interval where you want the formatted interval text to appear.'),
);
$form['granularity'] = array(
'#type' => 'number',
- '#title' => 'Interval',
+ '#title' => $this->t('Granularity'),
'#default_value' => $this->getSetting('granularity'),
'#description' => $this->t('How many time units should be shown in the formatted output.'),
);
@@ -171,7 +171,7 @@ public function settingsSummary() {
}
/**
- * Creates a formatted date value as a string.
+ * Formats a date/time as a time interval.
*
* @param \Drupal\Core\Datetime\DrupalDateTime|object $date
* A date/time object.