You are here

public function DateTimeFormatterBase::viewElements in Drupal 10

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

2 methods override DateTimeFormatterBase::viewElements()
DateTimeCustomFormatter::viewElements in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php
Builds a renderable array for a field value.
DateTimePlainFormatter::viewElements in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php
Builds a renderable array for a field value.

File

core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php, line 123

Class

DateTimeFormatterBase
Base class for 'DateTime Field formatter' plugin implementations.

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    if ($item->date) {

      /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
      $date = $item->date;
      $elements[$delta] = $this
        ->buildDateWithIsoAttribute($date);
      if (!empty($item->_attributes)) {
        $elements[$delta]['#attributes'] += $item->_attributes;

        // Unset field item attributes since they have been included in the
        // formatter output and should not be rendered in the field template.
        unset($item->_attributes);
      }
    }
  }
  return $elements;
}