You are here

public function TimestampFormatter::viewElements in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 168
Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter.

Class

TimestampFormatter
Plugin implementation of the 'timestamp' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  $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;
}