function 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 162 
Class
- TimestampFormatter
- Plugin implementation of the 'timestamp' formatter.
Namespace
Drupal\Core\Field\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $date_format = $this->getSetting('date_format');
  $custom_date_format = '';
  $timezone = $this->getSetting('timezone') ?: NULL;
  $langcode = NULL;
  // If an RFC2822 date format is requested, then the month and day have to
  // be in English. @see http://www.faqs.org/rfcs/rfc2822.html
  if ($date_format === 'custom' && ($custom_date_format = $this->getSetting('custom_date_format')) === 'r') {
    $langcode = 'en';
  }
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#cache' => [
        'contexts' => [
          'timezone',
        ],
      ],
      '#markup' => $this->dateFormatter
        ->format($item->value, $date_format, $custom_date_format, $timezone, $langcode),
    ];
  }
  return $elements;
}Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
