You are here

public function TimestampFormatter::viewElements in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::viewElements()
  2. 9 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 162

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 = '';
  $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;
}