MenuController.php

Same filename in this branch
  1. 11.x core/modules/menu_link_content/src/Controller/MenuController.php
Same filename in other branches
  1. 9 core/modules/menu_link_content/src/Controller/MenuController.php
  2. 9 core/modules/menu_ui/src/Controller/MenuController.php
  3. 8.9.x core/modules/menu_link_content/src/Controller/MenuController.php
  4. 8.9.x core/modules/menu_ui/src/Controller/MenuController.php
  5. 10 core/modules/menu_link_content/src/Controller/MenuController.php
  6. 10 core/modules/menu_ui/src/Controller/MenuController.php

Namespace

Drupal\menu_ui\Controller

File

core/modules/menu_ui/src/Controller/MenuController.php

View source
<?php

namespace Drupal\menu_ui\Controller;

use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Menu\MenuParentFormSelectorInterface;
use Drupal\system\MenuInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Returns responses for Menu routes.
 */
class MenuController extends ControllerBase {
    
    /**
     * The menu parent form service.
     *
     * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
     */
    protected $menuParentSelector;
    
    /**
     * Creates a new MenuController object.
     *
     * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_form
     *   The menu parent form service.
     */
    public function __construct(MenuParentFormSelectorInterface $menu_parent_form) {
        $this->menuParentSelector = $menu_parent_form;
    }
    
    /**
     * Gets all the available menus and menu items as a JavaScript array.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The request of the page.
     *
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     *   The available menu and menu items.
     */
    public function getParentOptions(Request $request) {
        $available_menus = [];
        if ($menus = $request->request
            ->all('menus')) {
            foreach ($menus as $menu) {
                $available_menus[$menu] = $menu;
            }
        }
        // @todo Update this to use the optional $cacheability parameter, so that
        //   a cacheable JSON response can be sent.
        $options = $this->menuParentSelector
            ->getParentSelectOptions('', $available_menus);
        return new JsonResponse($options);
    }
    
    /**
     * Route title callback.
     *
     * @param \Drupal\system\MenuInterface $menu
     *   The menu entity.
     *
     * @return array
     *   The menu label as a render array.
     */
    public function menuTitle(MenuInterface $menu) {
        return [
            '#markup' => $menu->label(),
            '#allowed_tags' => Xss::getHtmlTagList(),
        ];
    }

}

Classes

Title Deprecated Summary
MenuController Returns responses for Menu routes.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.