You are here

public function AuthorNameFormatter::viewElements in Drupal 10

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

Class

AuthorNameFormatter
Plugin implementation of the 'comment_username' formatter.

Namespace

Drupal\comment\Plugin\Field\FieldFormatter

Code

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

    /** @var \Drupal\comment\CommentInterface $comment */
    $comment = $item
      ->getEntity();
    $account = $comment
      ->getOwner();
    $elements[$delta] = [
      '#theme' => 'username',
      '#account' => $account,
      '#cache' => [
        'tags' => $account
          ->getCacheTags() + $comment
          ->getCacheTags(),
      ],
    ];
  }
  return $elements;
}