class AuthorNameFormatter
Same name and namespace in other branches
- 11.x core/modules/comment/src/Plugin/Field/FieldFormatter/AuthorNameFormatter.php \Drupal\comment\Plugin\Field\FieldFormatter\AuthorNameFormatter
- 10 core/modules/comment/src/Plugin/Field/FieldFormatter/AuthorNameFormatter.php \Drupal\comment\Plugin\Field\FieldFormatter\AuthorNameFormatter
- 8.9.x core/modules/comment/src/Plugin/Field/FieldFormatter/AuthorNameFormatter.php \Drupal\comment\Plugin\Field\FieldFormatter\AuthorNameFormatter
Plugin implementation of the 'comment_username' formatter.
Plugin annotation
@FieldFormatter(
id = "comment_username",
label = @Translation("Author name"),
description = @Translation("Display the author name."),
field_types = {
"string"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait extends \Drupal\Component\Plugin\PluginBase
- class \Drupal\Core\Field\PluginSettingsBase implements \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface extends \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Field\FormatterBase implements \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface extends \Drupal\Core\Field\PluginSettingsBase
- class \Drupal\comment\Plugin\Field\FieldFormatter\AuthorNameFormatter extends \Drupal\Core\Field\FormatterBase
- class \Drupal\Core\Field\FormatterBase implements \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface extends \Drupal\Core\Field\PluginSettingsBase
- class \Drupal\Core\Field\PluginSettingsBase implements \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface extends \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait extends \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of AuthorNameFormatter
File
-
core/
modules/ comment/ src/ Plugin/ Field/ FieldFormatter/ AuthorNameFormatter.php, line 21
Namespace
Drupal\comment\Plugin\Field\FieldFormatterView source
class AuthorNameFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition->getName() === 'name' && $field_definition->getTargetEntityTypeId() === 'comment';
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.