MenuLinks.php in GraphQL 8.4
File
src/Plugin/GraphQL/DataProducer/Menu/MenuLinks.php
View source
<?php
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\Menu;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Menu\MenuLinkInterface;
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 MenuLinks extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;
protected $menuLinkTree;
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('menu.link_tree'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, MenuLinkTreeInterface $menuLinkTree) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->menuLinkTree = $menuLinkTree;
}
public function resolve(MenuInterface $menu) {
$tree = $this->menuLinkTree
->load($menu
->id(), new MenuTreeParameters());
$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();
});
}
}