class TimestampFormatter
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter
- 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter
- 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter
Plugin implementation of the 'timestamp' formatter.
Attributes
#[FieldFormatter(id: 'timestamp', label: new TranslatableMarkup('Default'), field_types: [
'timestamp',
'created',
'changed',
])]
Hierarchy
- class \Drupal\Component\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Field\FormatterBase extends \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface implements \Drupal\Core\Field\PluginSettingsBase
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter implements \Drupal\Core\Field\FormatterBase
- class \Drupal\Core\Field\FormatterBase extends \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface implements \Drupal\Core\Field\PluginSettingsBase
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of TimestampFormatter
File
-
core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldFormatter/ TimestampFormatter.php, line 22
Namespace
Drupal\Core\Field\Plugin\Field\FieldFormatterView source
class TimestampFormatter extends FormatterBase {
/**
* Used to specify a date format that is customizable by user.
*
* @var string
*/
protected const CUSTOM_DATE_FORMAT = 'custom';
/**
* 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\DateFormatterInterface $dateFormatter
* The date formatter service.
* @param \Drupal\Core\Entity\EntityStorageInterface $dateFormatStorage
* The date format storage.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, protected DateFormatterInterface $dateFormatter, protected EntityStorageInterface $dateFormatStorage, protected TimeInterface $time) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
}
/**
* {@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_type.manager')
->getStorage('date_format'), $container->get('datetime.time'));
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'date_format' => 'medium',
'custom_date_format' => '',
'timezone' => '',
'tooltip' => [
'date_format' => 'long',
'custom_date_format' => '',
],
'time_diff' => [
'enabled' => FALSE,
'future_format' => '@interval hence',
'past_format' => '@interval ago',
'granularity' => 2,
'refresh' => 60,
],
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$date_formats = [];
$requestTime = $this->time
->getRequestTime();
foreach ($this->dateFormatStorage
->loadMultiple() as $machine_name => $value) {
$date_formats[$machine_name] = $this->t('@name format: @date', [
'@name' => $value->label(),
'@date' => $this->dateFormatter
->format($requestTime, $machine_name),
]);
}
$date_formats[static::CUSTOM_DATE_FORMAT] = $this->t('Custom');
$time_diff = $this->getSetting('time_diff');
$form['time_diff']['#tree'] = TRUE;
$form['time_diff']['enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t("Display as a time difference (e.g. '6 months ago')"),
'#default_value' => $time_diff['enabled'],
];
$states = $this->buildStates([
'time_diff',
'enabled',
], [
'checked' => TRUE,
]);
$form['time_diff']['future_format'] = [
'#type' => 'textfield',
'#title' => $this->t('Future format'),
'#description' => $this->t("Use the <code>@interval</code> placeholder to represent the formatted time difference interval. E.g. <code>@interval hence</code> will be displayed as <em>2 hours 5 minutes hence</em>."),
'#default_value' => $time_diff['future_format'],
'#states' => $states,
];
$form['time_diff']['past_format'] = [
'#type' => 'textfield',
'#title' => $this->t('Past format'),
'#description' => $this->t("Use the <code>@interval</code> placeholder to represent the formatted time difference interval. E.g. <code>@interval ago</code> will be displayed as <em>2 hours 5 minutes ago</em>."),
'#default_value' => $time_diff['past_format'],
'#states' => $states,
];
$form['time_diff']['granularity'] = [
'#type' => 'select',
'#title' => $this->t('Time units'),
'#description' => $this->t("How many time units will be used in formatting the time difference. For example, if '1' is selected then the displayed time difference will only contain a single time unit such as '2 years' or '5 minutes' never '2 years 3 months' or '5 minutes 8 seconds'."),
'#default_value' => $time_diff['granularity'],
'#options' => array_combine(range(1, 7), range(1, 7)),
'#states' => $states,
];
$form['time_diff']['refresh'] = [
'#type' => 'select',
'#title' => $this->t('Refresh interval'),
'#description' => $this->t('How often to refresh the displayed time difference. The time difference is refreshed on client-side, by JavaScript, without reloading the page.'),
'#default_value' => $time_diff['refresh'],
'#options' => $this->getRefreshIntervals(),
'#states' => $states,
];
$form['time_diff']['description'] = [
'#type' => 'item',
'#title' => $this->t('Fallback configuration'),
'#description' => $this->t('The configuration below is used as a fallback when JavaScript is not available on the page.'),
'#states' => $states,
];
$form['date_format'] = [
'#type' => 'select',
'#title' => $this->t('Date format'),
'#options' => $date_formats,
'#default_value' => $this->getSetting('date_format'),
];
$form['custom_date_format'] = [
'#type' => 'textfield',
'#title' => $this->t('Custom date format'),
'#description' => $this->t('See <a href="https://www.php.net/manual/datetime.format.php#refsect1-datetime.format-parameters" target="_blank">the documentation for PHP date formats</a>.'),
'#default_value' => $this->getSetting('custom_date_format'),
'#states' => $this->buildStates([
'date_format',
], [
'value' => static::CUSTOM_DATE_FORMAT,
]),
];
$form['timezone'] = [
'#type' => 'select',
'#title' => $this->t('Time zone'),
'#options' => [
'' => $this->t('- Default site/user time zone -'),
] + TimeZoneFormHelper::getOptionsListByRegion(),
'#default_value' => $this->getSetting('timezone'),
];
$tooltip = $this->getSetting('tooltip');
$form['tooltip']['#tree'] = TRUE;
$form['tooltip']['date_format'] = [
'#type' => 'select',
'#title' => $this->t('Tooltip date format'),
'#description' => $this->t('Select the date format to be used for the title and displayed on mouse hover.'),
'#options' => $date_formats,
'#default_value' => $tooltip['date_format'],
'#empty_option' => $this->t('- No tooltip -'),
];
$form['tooltip']['custom_date_format'] = [
'#type' => 'textfield',
'#title' => $this->t('Tooltip custom date format'),
'#description' => $this->t('See <a href="http://php.net/manual/function.date.php" target="_blank">the documentation for PHP date formats</a>.'),
'#default_value' => $tooltip['custom_date_format'],
'#states' => $this->buildStates([
'tooltip',
'date_format',
], [
'value' => static::CUSTOM_DATE_FORMAT,
]),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$time_diff = $this->getSetting('time_diff');
$date_format = $this->getSetting('date_format');
$date_format = $date_format === static::CUSTOM_DATE_FORMAT ? $this->getSetting('custom_date_format') : $date_format;
if ($time_diff['enabled']) {
$summary[] = $this->t('Displayed as a time difference');
$options = [
'granularity' => $time_diff['granularity'],
];
$timestamp = strtotime('1 year 1 month 1 week 1 day 1 hour 1 minute');
$interval = $this->dateFormatter
->formatTimeDiffUntil($timestamp, $options);
$display = new FormattableMarkup($time_diff['future_format'], [
'@interval' => $interval,
]);
$summary[] = $this->t('Future date: %display', [
'%display' => $display,
]);
$timestamp = strtotime('-1 year -1 month -1 week -1 day -1 hour -1 minute');
$interval = $this->dateFormatter
->formatTimeDiffSince($timestamp, $options);
$display = new FormattableMarkup($time_diff['past_format'], [
'@interval' => $interval,
]);
$summary[] = $this->t('Past date: %display', [
'%display' => $display,
]);
if ($time_diff['refresh']) {
$refresh_intervals = $this->getRefreshIntervals();
$summary[] = $this->t('Refresh every @interval', [
'@interval' => $refresh_intervals[$time_diff['refresh']],
]);
}
$summary[] = $this->t('Disabled JavaScript format: @date_format', [
'@date_format' => $date_format,
]);
}
else {
$summary[] = $this->t('Date format: @date_format', [
'@date_format' => $date_format,
]);
}
$tooltip = $this->getSetting('tooltip');
if (!empty($tooltip['date_format'])) {
$tooltip_date_format = $tooltip['date_format'];
$tooltip_date_format = $tooltip_date_format === static::CUSTOM_DATE_FORMAT ? $tooltip['custom_date_format'] : $tooltip_date_format;
$summary[] = $this->t('Tooltip date format: @date_format', [
'@date_format' => $tooltip_date_format,
]);
}
if ($timezone = $this->getSetting('timezone')) {
$summary[] = $this->t('Time zone: @timezone', [
'@timezone' => $timezone,
]);
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$date_format = $this->getSetting('date_format');
$custom_date_format = $this->getSetting('custom_date_format');
$timezone = $this->getSetting('timezone') ?: NULL;
$tooltip = $this->getSetting('tooltip');
$time_diff = $this->getSetting('time_diff');
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#theme' => 'time',
'#attributes' => [
// The representation of the date/time as RFC3339 "date-time".
// @see https://www.ietf.org/rfc/rfc3339.txt
'datetime' => $this->dateFormatter
->format($item->value, static::CUSTOM_DATE_FORMAT, \DateTimeInterface::RFC3339, $timezone),
],
'#text' => $this->dateFormatter
->format($item->value, $date_format, $custom_date_format, $timezone, $langcode),
'#cache' => [
'contexts' => [
'timezone',
],
],
];
if (!empty($tooltip['date_format'])) {
// Show a tooltip on mouse hover as title. When the time is displayed as
// time difference, it helps the user to read the exact date.
$elements[$delta]['#attributes']['title'] = $this->dateFormatter
->format($item->value, $tooltip['date_format'], $tooltip['custom_date_format'], $timezone, $langcode);
}
if ($time_diff['enabled']) {
$elements[$delta]['#attached']['library'][] = 'core/drupal.time-diff';
$settings = [
'format' => [
'future' => $time_diff['future_format'],
'past' => $time_diff['past_format'],
],
'granularity' => $time_diff['granularity'],
'refresh' => $time_diff['refresh'],
];
$elements[$delta]['#attributes']['data-drupal-time-diff'] = Json::encode($settings);
}
}
return $elements;
}
/**
* Builds the #states key for form elements.
*
* @param string[] $path
* The remote element path.
* @param array $conditions
* The conditions to be checked.
*
* @return array[]
* The #states array.
*/
protected function buildStates(array $path, array $conditions) : array {
$path = '[' . implode('][', $path) . ']';
return [
'visible' => [
[
":input[name='fields[{$this->fieldDefinition->getName()}][settings_edit_form][settings]{$path}']" => $conditions,
],
],
];
}
/**
* Returns the refresh interval options for the time difference display.
*
* @return \Drupal\Component\Render\MarkupInterface[]
* A list of refresh time intervals.
*/
protected function getRefreshIntervals() : array {
return [
0 => $this->t('No refresh'),
1 => $this->t('1 second'),
15 => $this->t('@count seconds', [
'@count' => 15,
]),
60 => $this->t('1 minute'),
300 => $this->t('@count minutes', [
'@count' => 5,
]),
600 => $this->t('@count minutes', [
'@count' => 10,
]),
];
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
DependencySerializationTrait::$_entityStorages | protected | property | An array of entity type IDs keyed by the property name of their storages. | |||
DependencySerializationTrait::$_serviceIds | protected | property | An array of service IDs keyed by property name used for serialization. | |||
DependencySerializationTrait::__sleep | public | function | 3 | |||
DependencySerializationTrait::__wakeup | public | function | 3 | |||
FormatterBase::$fieldDefinition | protected | property | The field definition. | |||
FormatterBase::$label | protected | property | The label display setting. | |||
FormatterBase::$settings | protected | property | The formatter settings. | Overrides PluginSettingsBase::$settings | ||
FormatterBase::$viewMode | protected | property | The view mode. | |||
FormatterBase::getFieldSetting | protected | function | Returns the value of a field setting. | |||
FormatterBase::getFieldSettings | protected | function | Returns the array of field settings. | |||
FormatterBase::isApplicable | public static | function | Returns if the formatter can be used for the provided field. | Overrides FormatterInterface::isApplicable | 12 | |
FormatterBase::prepareView | public | function | Allows formatters to load information for field values being displayed. | Overrides FormatterInterface::prepareView | 2 | |
FormatterBase::view | public | function | Builds a renderable array for a fully themed field. | Overrides FormatterInterface::view | 1 | |
MessengerTrait::$messenger | protected | property | The messenger. | 25 | ||
MessengerTrait::messenger | public | function | Gets the messenger. | 25 | ||
MessengerTrait::setMessenger | public | function | Sets the messenger. | |||
PluginBase::$configuration | protected | property | Configuration information passed into the plugin. | 1 | ||
PluginBase::$pluginDefinition | protected | property | The plugin implementation definition. | 1 | ||
PluginBase::$pluginId | protected | property | The plugin ID. | |||
PluginBase::DERIVATIVE_SEPARATOR | constant | A string which is used to separate base plugin IDs from the derivative ID. | ||||
PluginBase::getBaseId | public | function | Gets the base_plugin_id of the plugin instance. | Overrides DerivativeInspectionInterface::getBaseId | ||
PluginBase::getDerivativeId | public | function | Gets the derivative_id of the plugin instance. | Overrides DerivativeInspectionInterface::getDerivativeId | ||
PluginBase::getPluginDefinition | public | function | Gets the definition of the plugin implementation. | Overrides PluginInspectionInterface::getPluginDefinition | 2 | |
PluginBase::getPluginId | public | function | Gets the plugin ID of the plugin instance. | Overrides PluginInspectionInterface::getPluginId | ||
PluginBase::isConfigurable | Deprecated | public | function | Determines if the plugin is configurable. | ||
PluginSettingsBase::$defaultSettingsMerged | protected | property | Whether default settings have been merged into the current $settings. | |||
PluginSettingsBase::$thirdPartySettings | protected | property | The plugin settings injected by third party modules. | |||
PluginSettingsBase::calculateDependencies | public | function | Calculates dependencies for the configured plugin. | Overrides DependentPluginInterface::calculateDependencies | 6 | |
PluginSettingsBase::getSetting | public | function | Returns the value of a setting, or its default value if absent. | Overrides PluginSettingsInterface::getSetting | ||
PluginSettingsBase::getSettings | public | function | Returns the array of settings, including defaults for missing settings. | Overrides PluginSettingsInterface::getSettings | ||
PluginSettingsBase::getThirdPartyProviders | public | function | Gets the list of third parties that store information. | Overrides ThirdPartySettingsInterface::getThirdPartyProviders | ||
PluginSettingsBase::getThirdPartySetting | public | function | Gets the value of a third-party setting. | Overrides ThirdPartySettingsInterface::getThirdPartySetting | ||
PluginSettingsBase::getThirdPartySettings | public | function | Gets all third-party settings of a given module. | Overrides ThirdPartySettingsInterface::getThirdPartySettings | ||
PluginSettingsBase::mergeDefaults | protected | function | Merges default settings values into $settings. | |||
PluginSettingsBase::onDependencyRemoval | public | function | Informs the plugin that some configuration it depends on will be deleted. | Overrides PluginSettingsInterface::onDependencyRemoval | 3 | |
PluginSettingsBase::setSetting | public | function | Sets the value of a setting for the plugin. | Overrides PluginSettingsInterface::setSetting | ||
PluginSettingsBase::setSettings | public | function | Sets the settings for the plugin. | Overrides PluginSettingsInterface::setSettings | ||
PluginSettingsBase::setThirdPartySetting | public | function | Sets the value of a third-party setting. | Overrides ThirdPartySettingsInterface::setThirdPartySetting | ||
PluginSettingsBase::unsetThirdPartySetting | public | function | Unsets a third-party setting. | Overrides ThirdPartySettingsInterface::unsetThirdPartySetting | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | ||
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | |||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | |||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | |||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | ||
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | 1 | ||
TimestampFormatter::buildStates | protected | function | Builds the #states key for form elements. | |||
TimestampFormatter::create | public static | function | Creates an instance of the plugin. | Overrides FormatterBase::create | ||
TimestampFormatter::CUSTOM_DATE_FORMAT | protected | constant | Used to specify a date format that is customizable by user. | |||
TimestampFormatter::defaultSettings | public static | function | Defines the default settings for this plugin. | Overrides PluginSettingsBase::defaultSettings | ||
TimestampFormatter::getRefreshIntervals | protected | function | Returns the refresh interval options for the time difference display. | |||
TimestampFormatter::settingsForm | public | function | Returns a form to configure settings for the formatter. | Overrides FormatterBase::settingsForm | ||
TimestampFormatter::settingsSummary | public | function | Returns a short summary for the current formatter settings. | Overrides FormatterBase::settingsSummary | ||
TimestampFormatter::viewElements | public | function | Builds a renderable array for a field value. | Overrides FormatterInterface::viewElements | ||
TimestampFormatter::__construct | public | function | Constructs a new TimestampFormatter. | Overrides FormatterBase::__construct |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.