ChildOfMicrositeLookup.php in Entity Reference Hierarchy 3.x
File
modules/entity_hierarchy_microsite/src/ChildOfMicrositeLookup.php
View source
<?php
namespace Drupal\entity_hierarchy_microsite;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory;
use Drupal\entity_hierarchy\Storage\NestedSetStorageFactory;
use Drupal\node\NodeInterface;
use PNX\NestedSet\Node;
class ChildOfMicrositeLookup implements ChildOfMicrositeLookupInterface {
protected $entityFieldManager;
protected $entityTypeManager;
protected $nestedSetStorageFactory;
protected $nodeKeyFactory;
public function __construct(EntityFieldManagerInterface $entityFieldManager, EntityTypeManagerInterface $entityTypeManager, NestedSetStorageFactory $nestedSetStorageFactory, NestedSetNodeKeyFactory $nodeKeyFactory) {
$this->entityFieldManager = $entityFieldManager;
$this->entityTypeManager = $entityTypeManager;
$this->nestedSetStorageFactory = $nestedSetStorageFactory;
$this->nodeKeyFactory = $nodeKeyFactory;
}
public function findMicrositesForNodeAndField(NodeInterface $node, $field_name) {
$ids = [];
if ($node
->hasField($field_name) && !$node
->get($field_name)
->isEmpty()) {
$key = $this->nodeKeyFactory
->fromEntity($node);
$nestedSetStorage = $this->nestedSetStorageFactory
->get($field_name, 'node');
$ids = array_map(function (Node $treeNode) {
return $treeNode
->getId();
}, $nestedSetStorage
->findAncestors($key));
}
$ids[] = $node
->id();
$entityStorage = $this->entityTypeManager
->getStorage('entity_hierarchy_microsite');
return $entityStorage
->loadMultiple($entityStorage
->getQuery()
->sort('id')
->condition('home', $ids, 'IN')
->execute());
}
}