View source
<?php
namespace Drupal\entity_hierarchy_microsite;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Menu\Form\MenuLinkDefaultForm;
use Drupal\entity_hierarchy\Information\ParentCandidateInterface;
use Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface;
use Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory;
use Drupal\entity_hierarchy\Storage\NestedSetStorageFactory;
use Drupal\entity_hierarchy_microsite\Entity\MicrositeInterface;
use Drupal\entity_hierarchy_microsite\Form\MicrositeMenuItemForm;
use Drupal\entity_hierarchy_microsite\Plugin\Menu\MicrositeMenuItem;
class MicrositeMenuLinkDiscovery implements MicrositeMenuLinkDiscoveryInterface {
protected $nestedSetStorageFactory;
protected $mapper;
protected $candidate;
private $entityFieldManager;
private $entityTypeManager;
private $keyFactory;
protected $moduleHandler;
public function __construct(NestedSetStorageFactory $nestedSetStorageFactory, EntityTreeNodeMapperInterface $mapper, ParentCandidateInterface $candidate, EntityFieldManagerInterface $entityFieldManager, EntityTypeManagerInterface $entityTypeManager, NestedSetNodeKeyFactory $keyFactory, ModuleHandlerInterface $moduleHandler) {
$this->nestedSetStorageFactory = $nestedSetStorageFactory;
$this->mapper = $mapper;
$this->candidate = $candidate;
$this->entityFieldManager = $entityFieldManager;
$this->entityTypeManager = $entityTypeManager;
$this->keyFactory = $keyFactory;
$this->moduleHandler = $moduleHandler;
}
public function getMenuLinkDefintions(MicrositeInterface $microsite = NULL) {
$definitions = [];
$microsites = $microsite ? [
$microsite,
] : $this->entityTypeManager
->getStorage('entity_hierarchy_microsite')
->loadMultiple();
foreach ($microsites as $microsite) {
$home = $microsite
->getHome();
if (!$home) {
continue;
}
$key = $this->keyFactory
->fromEntity($home);
$parentUuids = [];
foreach ($this->candidate
->getCandidateFields($home) as $field_name) {
$tree = $this->nestedSetStorageFactory
->get($field_name, 'node');
$homeNode = $tree
->getNode($key);
if (!$homeNode) {
continue;
}
$nodes = $this->mapper
->loadEntitiesForTreeNodesWithoutAccessChecks('node', $tree
->findDescendants($key));
$url = $home
->toUrl();
$definitions[$home
->uuid()] = [
'class' => MicrositeMenuItem::class,
'menu_name' => 'entity-hierarchy-microsite',
'route_name' => $url
->getRouteName(),
'route_parameters' => $url
->getRouteParameters(),
'options' => $url
->getOptions(),
'title' => $home
->label(),
'description' => '',
'weight' => $homeNode
->getLeft(),
'id' => 'entity_hierarchy_microsite:' . $home
->uuid(),
'metadata' => [
'entity_id' => $home
->id(),
],
'form_class' => MenuLinkDefaultForm::class,
'enabled' => 1,
'expanded' => 1,
'provider' => 'entity_hierarchy_microsite',
'discovered' => 0,
];
foreach ($nodes as $treeNode) {
if (!$nodes
->contains($treeNode)) {
continue;
}
$item = $nodes
->offsetGet($treeNode);
$url = $item
->toUrl();
$revisionKey = sprintf('%s:%s', $treeNode
->getId(), $treeNode
->getRevisionId());
$itemUuid = $item
->uuid();
$parentUuids[$revisionKey] = $itemUuid;
$definitions[$itemUuid] = [
'class' => MicrositeMenuItem::class,
'menu_name' => 'entity-hierarchy-microsite',
'route_name' => $url
->getRouteName(),
'route_parameters' => $url
->getRouteParameters(),
'options' => $url
->getOptions(),
'title' => $item
->label(),
'description' => '',
'weight' => $treeNode
->getLeft(),
'id' => 'entity_hierarchy_microsite:' . $itemUuid,
'metadata' => [
'entity_id' => $item
->id(),
],
'form_class' => MenuLinkDefaultForm::class,
'enabled' => 1,
'expanded' => 1,
'provider' => 'entity_hierarchy_microsite',
'discovered' => 0,
'parent' => 'entity_hierarchy_microsite:' . $home
->uuid(),
];
$parent = $tree
->findParent($treeNode
->getNodeKey());
if ($parent && ($parentRevisionKey = sprintf('%s:%s', $parent
->getId(), $parent
->getRevisionId())) && array_key_exists($parentRevisionKey, $parentUuids)) {
$definitions[$itemUuid]['parent'] = 'entity_hierarchy_microsite:' . $parentUuids[$parentRevisionKey];
}
}
}
if ($definitions) {
foreach ($this->entityTypeManager
->getStorage('eh_microsite_menu_override')
->loadByProperties([
'target' => array_keys($definitions),
]) as $override) {
$original = $definitions[$override
->getTarget()];
$definitions[$override
->getTarget()] = [
'metadata' => [
'original' => array_intersect_key($original, [
'title' => TRUE,
'weight' => TRUE,
'enabled' => TRUE,
'expanded' => TRUE,
'parent' => TRUE,
]),
] + $original['metadata'],
'title' => $override
->label(),
'form_class' => MicrositeMenuItemForm::class,
'weight' => $override
->getWeight(),
'enabled' => $override
->isEnabled(),
'expanded' => $override
->isExpanded(),
'parent' => $override
->getParent(),
] + $original;
}
}
}
$this->moduleHandler
->alter('entity_hierarchy_microsite_links', $definitions);
return $definitions;
}
}