FileFormatterBase.php

Same filename and directory in other branches
  1. 8.9.x core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php
  2. 10 core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php
  3. 11.x core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php

Namespace

Drupal\file\Plugin\Field\FieldFormatter

File

core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php

View source
<?php

namespace Drupal\file\Plugin\Field\FieldFormatter;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;

/**
 * Base class for file formatters.
 */
abstract class FileFormatterBase extends EntityReferenceFormatterBase {
    
    /**
     * {@inheritdoc}
     */
    protected function needsEntityLoad(EntityReferenceItem $item) {
        return parent::needsEntityLoad($item) && $item->isDisplayed();
    }
    
    /**
     * {@inheritdoc}
     */
    protected function checkAccess(EntityInterface $entity) {
        // Only check access if the current file access control handler explicitly
        // opts in by implementing FileAccessFormatterControlHandlerInterface.
        $access_handler_class = $entity->getEntityType()
            ->getHandlerClass('access');
        if (is_subclass_of($access_handler_class, '\\Drupal\\file\\FileAccessFormatterControlHandlerInterface')) {
            return $entity->access('view', NULL, TRUE);
        }
        else {
            return AccessResult::allowed();
        }
    }

}

Classes

Title Deprecated Summary
FileFormatterBase Base class for file formatters.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.