Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / comment / src / Plugin / Field / FieldFormatter / AuthorNameFormatter.php
1 <?php
2
3 namespace Drupal\comment\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FormatterBase;
8
9 /**
10  * Plugin implementation of the 'comment_username' formatter.
11  *
12  * @FieldFormatter(
13  *   id = "comment_username",
14  *   label = @Translation("Author name"),
15  *   description = @Translation("Display the author name."),
16  *   field_types = {
17  *     "string"
18  *   }
19  * )
20  */
21 class AuthorNameFormatter extends FormatterBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function viewElements(FieldItemListInterface $items, $langcode) {
27     $elements = [];
28
29     foreach ($items as $delta => $item) {
30       /** @var $comment \Drupal\comment\CommentInterface */
31       $comment = $item->getEntity();
32       $account = $comment->getOwner();
33       $elements[$delta] = [
34         '#theme' => 'username',
35         '#account' => $account,
36         '#cache' => [
37           'tags' => $account->getCacheTags() + $comment->getCacheTags(),
38         ],
39       ];
40     }
41
42     return $elements;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function isApplicable(FieldDefinitionInterface $field_definition) {
49     return $field_definition->getName() === 'name' && $field_definition->getTargetEntityTypeId() === 'comment';
50   }
51
52 }