function AuthorFormatter::viewElements

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

Overrides FormatterInterface::viewElements

File

core/modules/user/src/Plugin/Field/FieldFormatter/AuthorFormatter.php, line 27

Class

AuthorFormatter
Plugin implementation of the 'author' formatter.

Namespace

Drupal\user\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
        $elements[$delta] = [
            '#theme' => 'username',
            '#account' => $entity,
            '#link_options' => [
                'attributes' => [
                    'rel' => 'author',
                ],
            ],
            '#cache' => [
                'tags' => $entity->getCacheTags(),
            ],
        ];
    }
    return $elements;
}

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