UltimenuController.php in Ultimenu 8.2
File
src/Controller/UltimenuController.php
View source
<?php
namespace Drupal\ultimenu\Controller;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\ultimenu\UltimenuManagerInterface;
use Drupal\ultimenu\Ajax\UltimenuHtmlCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class UltimenuController extends ControllerBase {
protected $contextHandler;
protected $contextRepository;
protected $menuLinkManager;
protected $ultimenuManager;
protected $frontPage;
protected $currentPath;
public function __construct(ContextHandlerInterface $context_handler, ContextRepositoryInterface $context_repository, MenuLinkManagerInterface $menu_link_manager, UltimenuManagerInterface $ultimenu_manager) {
$this->contextHandler = $context_handler;
$this->contextRepository = $context_repository;
$this->menuLinkManager = $menu_link_manager;
$this->ultimenuManager = $ultimenu_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('context.handler'), $container
->get('context.repository'), $container
->get('plugin.manager.menu.link'), $container
->get('ultimenu.manager'));
}
public function load(Request $request) {
$mlid = $request->query
->get('mlid');
$sub = $request->query
->get('sub');
if (isset($mlid) && ($regions = $this->ultimenuManager
->getSetting('regions'))) {
$link = $this->menuLinkManager
->createInstance($mlid);
if (!$link || !$link
->isEnabled()) {
throw new NotFoundHttpException();
}
$response = new AjaxResponse();
$rid = $this->ultimenuManager
->getTool()
->getRegionKey($link);
if (!empty($regions[$rid])) {
$menu_name = $link
->getMenuName();
$plugin_id = 'ultimenu_block:ultimenu-' . $menu_name;
$block = $this->ultimenuManager
->getEntityTypeManager()
->getStorage('block')
->loadByProperties([
'plugin' => $plugin_id,
]);
$block = reset($block);
$config = $block
->get('settings');
$config['current_path'] = $this
->getCurrentPath($request);
$config['has_submenu'] = $sub;
$config['menu_name'] = $menu_name;
$config['mlid'] = $mlid;
$config['title'] = $this->ultimenuManager
->getTool()
->getTitle($link);
$context = new RenderContext();
$render = $this->ultimenuManager
->getRenderer()
->executeInRenderContext($context, function () use ($rid, $config) {
return $this->ultimenuManager
->buildFlyout($rid, $config);
});
if ($render) {
if (!$context
->isEmpty()) {
$bubbleable_metadata = $context
->pop();
BubbleableMetadata::createFromRenderArray($render)
->merge($bubbleable_metadata)
->applyTo($render);
}
$response
->addCommand(new UltimenuHtmlCommand('[data-ultiajax-region="' . $rid . '"]', $render, NULL, 'region'));
}
return $response;
}
throw new AccessDeniedHttpException();
}
throw new NotFoundHttpException();
}
protected function getFrontPagePath() {
if (!isset($this->frontPage)) {
$this->frontPage = $this->ultimenuManager
->getConfig('system.site')
->get('page.front');
}
return $this->frontPage;
}
protected function getCurrentPath(Request $request) {
if (!isset($this->currentPath)) {
$referer = $request->headers
->get('Referer', '');
$url = parse_url($referer);
$this->currentPath = $url['path'] == '/' ? $this
->getFrontPagePath() : $url['path'];
}
return $this->currentPath;
}
}