3 namespace Drupal\comment\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FormatterBase;
10 * Plugin implementation of the 'comment_username' formatter.
13 * id = "comment_username",
14 * label = @Translation("Author name"),
15 * description = @Translation("Display the author name."),
21 class AuthorNameFormatter extends FormatterBase {
26 public function viewElements(FieldItemListInterface $items, $langcode) {
29 foreach ($items as $delta => $item) {
30 /** @var $comment \Drupal\comment\CommentInterface */
31 $comment = $item->getEntity();
32 $account = $comment->getOwner();
34 '#theme' => 'username',
35 '#account' => $account,
37 'tags' => $account->getCacheTags() + $comment->getCacheTags(),
48 public static function isApplicable(FieldDefinitionInterface $field_definition) {
49 return $field_definition->getName() === 'name' && $field_definition->getTargetEntityTypeId() === 'comment';