Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions modules/custom/az_core/src/Controller/MobileNavController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Drupal\Core\Cache\CacheableAjaxResponse;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Defines a controller to update a mobile nav block.
Expand All @@ -23,22 +25,33 @@ class MobileNavController implements ContainerInjectionInterface {
*/
protected $blockManager;

/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;

/**
* MobileNavController constructor.
*
* @param \Drupal\Core\Block\BlockManager $block_manager
* The block manager.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(BlockManager $block_manager) {
public function __construct(BlockManager $block_manager, RequestStack $request_stack) {
$this->blockManager = $block_manager;
$this->requestStack = $request_stack;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.block')
$container->get('plugin.manager.block'),
$container->get('request_stack')
);
}

Expand All @@ -54,6 +67,11 @@ public static function create(ContainerInterface $container) {
* An CacheableAjaxResponse object.
*/
public function mobileNavCallback($menu_id, $menu_root = '') {
$request = $this->requestStack->getCurrentRequest();
if (!$request || !$request->isXmlHttpRequest()) {
throw new NotFoundHttpException();
}

$mobile_nav_block = $this->blockManager->createInstance('mobile_nav_block',
[
'menu_id' => $menu_id,
Expand Down
Loading