MenuController.php 2.93 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\menu\Controller\MenuController.
 */

namespace Drupal\menu\Controller;

10
use Drupal\Component\Utility\Xss;
11
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
12
use Drupal\Core\Entity\EntityManagerInterface;
13 14 15 16 17 18 19 20 21
use Drupal\menu_link\MenuLinkStorageControllerInterface;
use Drupal\system\MenuInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Returns responses for Menu routes.
 */
22
class MenuController implements ContainerInjectionInterface {
23 24 25 26 27 28 29 30 31 32 33

  /**
   * The menu link storage.
   *
   * @var \Drupal\menu_link\MenuLinkStorageControllerInterface
   */
  protected $menuLinkStorage;

  /**
   * The entity manager.
   *
34
   * @var \Drupal\Core\Entity\EntityManagerInterface
35 36 37 38 39 40 41 42
   */
  protected $entityManager;

  /**
   * Constructs a new MenuController.
   *
   * @param \Drupal\menu_link\MenuLinkStorageControllerInterface $menu_link_storage
   *   The storage controller.
43
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
44 45
   *   The entity manager.
   */
46
  public function __construct(MenuLinkStorageControllerInterface $menu_link_storage, EntityManagerInterface $entity_manager) {
47 48 49 50 51 52 53 54 55
    $this->menuLinkStorage = $menu_link_storage;
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
56 57
      $container->get('entity.manager')->getStorageController('menu_link'),
      $container->get('entity.manager')
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    );
  }

  /**
   * 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 = array();
    if ($menus = $request->request->get('menus')) {
      foreach ($menus as $menu) {
        $available_menus[$menu] = $menu;
      }
    }
    $options = _menu_get_options(menu_get_menus(), $available_menus, array('mlid' => 0));

    return new JsonResponse($options);
  }

  /**
   * Provides the menu link submission form.
   *
   * @param \Drupal\system\MenuInterface $menu
   *   An entity representing a custom menu.
   *
   * @return array
   *   Returns the menu link submission form.
   */
  public function addLink(MenuInterface $menu) {
    $menu_link = $this->menuLinkStorage->create(array(
      'mlid' => 0,
      'plid' => 0,
      'menu_name' => $menu->id(),
    ));
    return $this->entityManager->getForm($menu_link);
  }

100 101 102 103 104 105 106 107 108 109 110 111 112
  /**
   * Route title callback.
   *
   * @param \Drupal\system\MenuInterface $menu
   *   The menu entity.
   *
   * @return string
   *   The menu label.
   */
  public function menuTitle(MenuInterface $menu) {
    return Xss::filter($menu->label());
  }

113
}