You are here

public function AuthorFormatter::viewElements in Drupal 9

Same name and namespace in other branches
  1. 8 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()

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