function TimestampFormatter::viewElements

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::viewElements()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::viewElements()
  3. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::viewElements()

Overrides FormatterInterface::viewElements

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php, line 284

Class

TimestampFormatter
Plugin implementation of the 'timestamp' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.