You are here

public function AuthorFormatter::viewElements in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 33
Contains \Drupal\user\Plugin\Field\FieldFormatter\AuthorFormatter.

Class

AuthorFormatter
Plugin implementation of the 'author' formatter.

Namespace

Drupal\user\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {

    /** @var $referenced_user \Drupal\user\UserInterface */
    $elements[$delta] = array(
      '#theme' => 'username',
      '#account' => $entity,
      '#link_options' => array(
        'attributes' => array(
          'rel' => 'author',
        ),
      ),
      '#cache' => array(
        'tags' => $entity
          ->getCacheTags(),
      ),
    );
  }
  return $elements;
}