needsEntityLoad example

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);
    }
    // "multiple entity load" to load all the entities for the multiple     // "entity reference item lists" being displayed. We thus cannot use     // \Drupal\Core\Field\EntityReferenceFieldItemList::referencedEntities().     $ids = [];
    foreach ($entities_items as $items) {
      foreach ($items as $item) {
        // To avoid trying to reload non-existent entities in         // getEntitiesToView(), explicitly mark the items where $item->entity         // contains a valid entity ready for display. All items are initialized         // at FALSE.         $item->_loaded = FALSE;
        if ($this->needsEntityLoad($item)) {
          $ids[] = $item->target_id;
        }
      }
    }
    if ($ids) {
      $target_type = $this->getFieldSetting('target_type');
      $target_entities = \Drupal::entityTypeManager()->getStorage($target_type)->loadMultiple($ids);
    }

    // For each item, pre-populate the loaded entity in $item->entity, and set     // the 'loaded' flag.
Home | Imprint | This part of the site doesn't use cookies.