EntityTreeNodeMapper.php in Entity Reference Hierarchy 8.2
File
src/Storage/EntityTreeNodeMapper.php
View source
<?php
namespace Drupal\entity_hierarchy\Storage;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use PNX\NestedSet\Node;
class EntityTreeNodeMapper implements EntityTreeNodeMapperInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
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()) {
continue;
}
$loadedEntities[$node] = $entity;
if ($cache) {
$cache
->addCacheableDependency($entity);
}
}
return $loadedEntities;
}
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')) {
$entities
->offsetUnset($node);
}
}
return $entities;
}
}