You are here

public function DateTimeTimeAgoFormatter::viewElements in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeTimeAgoFormatter::viewElements()
  2. 10 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeTimeAgoFormatter::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 TimestampAgoFormatter::viewElements

File

core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php, line 25

Class

DateTimeTimeAgoFormatter
Plugin implementation of the 'Time ago' formatter for 'datetime' fields.

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $date = $item->date;
    $output = [];
    if (!empty($item->date)) {
      $output = $this
        ->formatDate($date);
    }
    $elements[$delta] = $output;
  }
  return $elements;
}