You are here

public function NodeAuthor::build in Display Suite 8.4

Same name and namespace in other branches
  1. 8.2 src/Plugin/DsField/Node/NodeAuthor.php \Drupal\ds\Plugin\DsField\Node\NodeAuthor::build()
  2. 8.3 src/Plugin/DsField/Node/NodeAuthor.php \Drupal\ds\Plugin\DsField\Node\NodeAuthor::build()

Renders a field.

Return value

array A renderable array representing the content of the field.

Overrides DsFieldBase::build

File

src/Plugin/DsField/Node/NodeAuthor.php, line 22

Class

NodeAuthor
Plugin that renders the author of a node.

Namespace

Drupal\ds\Plugin\DsField\Node

Code

public function build() {

  /* @var $node NodeInterface */
  $node = $this
    ->entity();

  /* @var $user UserInterface */
  $user = $node
    ->getOwner();

  // Users without a user name are anonymous users. These are never linked.
  if (empty($user->name)) {
    return [
      '#plain_text' => \Drupal::config('user.settings')
        ->get('anonymous'),
    ];
  }
  $field = $this
    ->getFieldConfiguration();
  if ($field['formatter'] == 'author') {
    return [
      '#markup' => $user
        ->getAccountName(),
      '#cache' => [
        'tags' => $user
          ->getCacheTags(),
      ],
    ];
  }
  if ($field['formatter'] == 'author_linked') {
    return [
      '#theme' => 'username',
      '#account' => $user,
      '#cache' => [
        'tags' => $user
          ->getCacheTags(),
      ],
    ];
  }

  // Otherwise return an empty array.
  return [];
}