You are here

class EntityTreeNodeMapper in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Storage/EntityTreeNodeMapper.php \Drupal\entity_hierarchy\Storage\EntityTreeNodeMapper

Takes an array of tree nodes & returns matching entities, keyed by tree node.

Hierarchy

Expanded class hierarchy of EntityTreeNodeMapper

1 string reference to 'EntityTreeNodeMapper'
entity_hierarchy.services.yml in ./entity_hierarchy.services.yml
entity_hierarchy.services.yml
1 service uses EntityTreeNodeMapper
entity_hierarchy.entity_tree_node_mapper in ./entity_hierarchy.services.yml
Drupal\entity_hierarchy\Storage\EntityTreeNodeMapper

File

src/Storage/EntityTreeNodeMapper.php, line 12

Namespace

Drupal\entity_hierarchy\Storage
View source
class EntityTreeNodeMapper implements EntityTreeNodeMapperInterface {

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new EntityTreeNodeMapper object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public function loadEntitiesForTreeNodesWithoutAccessChecks($entity_type_id, array $nodes, RefinableCacheableDependencyInterface $cache = NULL) {
    $entities = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->loadMultiple(array_map(function (Node $node) {
      return $node
        ->getId();
    }, $nodes));
    $loadedEntities = new \SplObjectStorage();
    foreach ($nodes as $node) {
      $nodeId = $node
        ->getId();
      $entity = isset($entities[$nodeId]) ? $entities[$nodeId] : FALSE;
      if (!$entity || $entity
        ->getEntityType()
        ->hasKey('revision') && $node
        ->getRevisionId() != $entity
        ->getRevisionId()) {

        // Bypass non default revisions and deleted items.
        continue;
      }
      $loadedEntities[$node] = $entity;
      if ($cache) {
        $cache
          ->addCacheableDependency($entity);
      }
    }
    return $loadedEntities;
  }

  /**
   * {@inheritdoc}
   */
  public function loadAndAccessCheckEntitysForTreeNodes($entity_type_id, array $nodes, RefinableCacheableDependencyInterface $cache = NULL) {
    $entities = $this
      ->loadEntitiesForTreeNodesWithoutAccessChecks($entity_type_id, $nodes, $cache);
    foreach ($nodes as $node) {
      if (!$entities
        ->offsetExists($node)) {
        continue;
      }
      if (($entity = $entities
        ->offsetGet($node)) && !$entity
        ->access('view label')) {

        // Check access.
        $entities
          ->offsetUnset($node);
      }
    }
    return $entities;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityTreeNodeMapper::$entityTypeManager protected property Entity type manager.
EntityTreeNodeMapper::loadAndAccessCheckEntitysForTreeNodes public function Loads Drupal entities for given tree nodes and checks access. Overrides EntityTreeNodeMapperInterface::loadAndAccessCheckEntitysForTreeNodes
EntityTreeNodeMapper::loadEntitiesForTreeNodesWithoutAccessChecks public function Loads Drupal entities for given tree nodes. Overrides EntityTreeNodeMapperInterface::loadEntitiesForTreeNodesWithoutAccessChecks
EntityTreeNodeMapper::__construct public function Constructs a new EntityTreeNodeMapper object.