3 namespace Drupal\file\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
8 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
11 * Base class for file formatters.
13 abstract class FileFormatterBase extends EntityReferenceFormatterBase {
18 protected function needsEntityLoad(EntityReferenceItem $item) {
19 return parent::needsEntityLoad($item) && $item->isDisplayed();
25 protected function checkAccess(EntityInterface $entity) {
26 // Only check access if the current file access control handler explicitly
27 // opts in by implementing FileAccessFormatterControlHandlerInterface.
28 $access_handler_class = $entity->getEntityType()->getHandlerClass('access');
29 if (is_subclass_of($access_handler_class, '\Drupal\file\FileAccessFormatterControlHandlerInterface')) {
30 return $entity->access('view', NULL, TRUE);
33 return AccessResult::allowed();