function DateTimeFormatterBase::viewElements

Same name in other branches
  1. 8.9.x core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::viewElements()
  2. 10 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::viewElements()
  3. 11.x core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::viewElements()

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.