MenuLinksActiveTrail.php in Thunder 6.2.x
File
modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/MenuLinksActiveTrail.php
View source
<?php
namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Menu\MenuLinkInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Menu\MenuLinkTreeElement;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\system\MenuInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuLinksActiveTrail extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;
protected $menuLinkTree;
protected $menuLinkManager;
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('menu.link_tree'), $container
->get('plugin.manager.menu.link'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, MenuLinkTreeInterface $menuLinkTree, MenuLinkManagerInterface $menuLinkManager) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->menuLinkTree = $menuLinkTree;
$this->menuLinkManager = $menuLinkManager;
}
public function resolve(MenuInterface $menu, ContentEntityInterface $entity) {
$parameters = new MenuTreeParameters();
$links = $this->menuLinkManager
->loadLinksByRoute($entity
->toUrl()
->getRouteName(), $entity
->toUrl()
->getRouteParameters(), $menu
->id());
$activeLink = reset($links);
if ($activeLink) {
$activeTrail = [
'' => '',
];
if ($parents = $this->menuLinkManager
->getParentIds($activeLink
->getPluginId())) {
$activeTrail = $parents + $activeTrail;
}
$parameters
->setActiveTrail($activeTrail);
}
$tree = $this->menuLinkTree
->load($menu
->id(), $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
return array_filter($this->menuLinkTree
->transform($tree, $manipulators), function (MenuLinkTreeElement $item) {
return $item->link instanceof MenuLinkInterface && $item->link
->isEnabled();
});
}
}