ChildEntityWarningBuilder.php in Entity Reference Hierarchy 8.2
File
src/Information/ChildEntityWarningBuilder.php
View source
<?php
namespace Drupal\entity_hierarchy\Information;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface;
use Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory;
use Drupal\entity_hierarchy\Storage\NestedSetStorageFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ChildEntityWarningBuilder implements ContainerInjectionInterface {
protected $parentCandidate;
protected $treeNodeMapper;
protected $nestedSetStorageFactory;
protected $nodeKeyFactory;
public function __construct(ParentCandidateInterface $parentCandidate, EntityTreeNodeMapperInterface $treeNodeMapper, NestedSetStorageFactory $nestedSetStorageFactory, NestedSetNodeKeyFactory $nodeKeyFactory) {
$this->parentCandidate = $parentCandidate;
$this->treeNodeMapper = $treeNodeMapper;
$this->nestedSetStorageFactory = $nestedSetStorageFactory;
$this->nodeKeyFactory = $nodeKeyFactory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_hierarchy.information.parent_candidate'), $container
->get('entity_hierarchy.entity_tree_node_mapper'), $container
->get('entity_hierarchy.nested_set_storage_factory'), $container
->get('entity_hierarchy.nested_set_node_factory'));
}
public function buildChildEntityWarnings(ContentEntityInterface $parent) {
$return = [];
if ($fields = $this->parentCandidate
->getCandidateFields($parent)) {
$cache = new CacheableMetadata();
foreach ($fields as $field_name) {
$storage = $this->nestedSetStorageFactory
->get($field_name, $parent
->getEntityTypeId());
$nodeKey = $this->nodeKeyFactory
->fromEntity($parent);
$children = $storage
->findChildren($nodeKey);
if ($parent_node = $storage
->findParent($nodeKey)) {
$children[] = $parent_node;
}
$entities = $this->treeNodeMapper
->loadAndAccessCheckEntitysForTreeNodes($parent
->getEntityTypeId(), $children, $cache);
$return[] = new ChildEntityWarning($entities, $cache, $parent_node);
}
}
return $return;
}
}