Same filename and directory in other branches
- 10 core/modules/user/src/Plugin/Field/FieldFormatter/AuthorFormatter.php
- 9 core/modules/user/src/Plugin/Field/FieldFormatter/AuthorFormatter.php
Namespace
Drupal\user\Plugin\Field\FieldFormatter
File
core/modules/user/src/Plugin/Field/FieldFormatter/AuthorFormatter.php
View source
<?php
namespace Drupal\user\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
class AuthorFormatter extends EntityReferenceFormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
$elements[$delta] = [
'#theme' => 'username',
'#account' => $entity,
'#link_options' => [
'attributes' => [
'rel' => 'author',
],
],
'#cache' => [
'tags' => $entity
->getCacheTags(),
],
];
}
return $elements;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition
->getFieldStorageDefinition()
->getSetting('target_type') == 'user';
}
protected function checkAccess(EntityInterface $entity) {
return $entity
->access('view label', NULL, TRUE);
}
}
Classes