Version 1
[yaffs-website] / web / core / modules / file / src / Plugin / Field / FieldFormatter / FileFormatterBase.php
1 <?php
2
3 namespace Drupal\file\Plugin\Field\FieldFormatter;
4
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;
9
10 /**
11  * Base class for file formatters.
12  */
13 abstract class FileFormatterBase extends EntityReferenceFormatterBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected function needsEntityLoad(EntityReferenceItem $item) {
19     return parent::needsEntityLoad($item) && $item->isDisplayed();
20   }
21
22   /**
23    * {@inheritdoc}
24    */
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);
31     }
32     else {
33       return AccessResult::allowed();
34     }
35   }
36
37 }