class MenuController

Same name in this branch
  1. 9 core/modules/menu_link_content/src/Controller/MenuController.php \Drupal\menu_link_content\Controller\MenuController
Same name and namespace in other branches
  1. 11.x core/modules/menu_link_content/src/Controller/MenuController.php \Drupal\menu_link_content\Controller\MenuController
  2. 11.x core/modules/menu_ui/src/Controller/MenuController.php \Drupal\menu_ui\Controller\MenuController

Returns responses for Menu routes.

Hierarchy

Expanded class hierarchy of MenuController

File

core/modules/menu_ui/src/Controller/MenuController.php, line 16

Namespace

Drupal\menu_ui\Controller
View source
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;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('menu.parent_form_selector'));
  }
  
  /**
   * 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
      ->get('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(),
    ];
  }

}

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