View source
<?php
namespace Drupal\coffee\Controller;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Menu\LocalTaskManagerInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class CoffeeController extends ControllerBase {
protected $config;
protected $menuLinkTree;
protected $localTaskManager;
protected $accessManager;
protected $urlGenerator;
protected $routeMatch;
public function __construct(ConfigFactoryInterface $config_factory, MenuLinkTreeInterface $menu_link_tree, LocalTaskManagerInterface $local_task_manager, AccessManagerInterface $access_manager, UrlGeneratorInterface $url_generator, RouteMatchInterface $route_match) {
$this->config = $config_factory
->get('coffee.configuration');
$this->menuLinkTree = $menu_link_tree;
$this->localTaskManager = $local_task_manager;
$this->accessManager = $access_manager;
$this->urlGenerator = $url_generator;
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('menu.link_tree'), $container
->get('plugin.manager.menu.local_task'), $container
->get('access_manager'), $container
->get('coffee.url_generator'), $container
->get('current_route_match'));
}
public function coffeeData() {
$output = [];
foreach ($this->config
->get('coffee_menus') as $menu_name) {
$tree = $this
->getMenuTreeElements($menu_name);
$commands_group = $menu_name == 'account' ? ':user' : NULL;
foreach ($tree as $tree_element) {
$link = $tree_element->link;
$output[$link
->getRouteName()] = [
'value' => $link
->getUrlObject()
->setUrlGenerator($this->urlGenerator)
->toString(),
'label' => $link
->getTitle(),
'command' => $commands_group,
];
$tasks = $this
->getLocalTasksForRoute($link
->getRouteName(), $link
->getRouteParameters());
foreach ($tasks as $route_name => $task) {
if (empty($output[$route_name])) {
$output[$route_name] = [
'value' => $task['url']
->setUrlGenerator($this->urlGenerator)
->toString(),
'label' => $link
->getTitle() . ' - ' . $task['title'],
'command' => NULL,
];
}
}
}
}
$commands = $this
->moduleHandler()
->invokeAll('coffee_commands');
if (!empty($commands)) {
$output = array_merge($output, $commands);
}
$output = array_values($output);
return new JsonResponse($output);
}
protected function getMenuTreeElements($menu_name) {
$parameters = new MenuTreeParameters();
$tree = $this->menuLinkTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkNodeAccess',
],
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
[
'callable' => 'menu.default_tree_manipulators:flatten',
],
];
$tree = $this->menuLinkTree
->transform($tree, $manipulators);
foreach ($tree as $key => $element) {
if (!$element->access
->isAllowed()) {
unset($tree[$key]);
}
}
return $tree;
}
protected function getLocalTasksForRoute($route_name, array $route_parameters) {
$links = [];
$tree = $this->localTaskManager
->getLocalTasksForRoute($route_name);
foreach ($tree as $instances) {
foreach ($instances as $child) {
$child_route_name = $child
->getRouteName();
$child_route_parameters = $child
->getRouteParameters($this->routeMatch) + $route_parameters;
if (strpos($child_route_name, 'config_translate') !== FALSE && $this->accessManager
->checkNamedRoute($child_route_name, $child_route_parameters)) {
$links[$child_route_name] = [
'title' => $child
->getTitle(),
'url' => Url::fromRoute($child_route_name, $child_route_parameters),
'localized_options' => $child
->getOptions($this->routeMatch),
];
}
}
}
return $links;
}
}