function TimestampFormatter::viewElements

Same name and namespace 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. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

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

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.