Commit c5f81fb0 authored by alexpott's avatar alexpott

Issue #1984702 by tim.plunkett, dawehner: Convert menu.module's page callbacks to Controllers.

parent 7f025440
<?php
/**
* @file
* Contains \Drupal\menu\Access\DeleteMenuAccessCheck.
*/
namespace Drupal\menu\Access;
use Drupal\Core\Access\StaticAccessCheckInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Access check for menu delete routes.
*/
class DeleteMenuAccessCheck implements StaticAccessCheckInterface {
/**
* {@inheritdoc}
*/
public function appliesTo() {
return array('_access_menu_delete_menu');
}
/**
* {@inheritdoc}
*/
public function access(Route $route, Request $request) {
if (user_access('administer menu') && $menu = $request->attributes->get('menu')) {
// System-defined menus may not be deleted.
$system_menus = menu_list_system_menus();
return !isset($system_menus[$menu->id()]);
}
return FALSE;
}
}
<?php
/**
* @file
* Contains \Drupal\menu\Controller\MenuController.
*/
namespace Drupal\menu\Controller;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Entity\EntityManager;
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.
*/
class MenuController implements ControllerInterface {
/**
* The menu link storage.
*
* @var \Drupal\menu_link\MenuLinkStorageControllerInterface
*/
protected $menuLinkStorage;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* Constructs a new MenuController.
*
* @param \Drupal\menu_link\MenuLinkStorageControllerInterface $menu_link_storage
* The storage controller.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
*/
public function __construct(MenuLinkStorageControllerInterface $menu_link_storage, EntityManager $entity_manager) {
$this->menuLinkStorage = $menu_link_storage;
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity')->getStorageController('menu_link'),
$container->get('plugin.manager.entity')
);
}
/**
* 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) {
// @todo Remove this when https://drupal.org/node/1981644 is in.
drupal_set_title(t('Add menu link'));
$menu_link = $this->menuLinkStorage->create(array(
'mlid' => 0,
'plid' => 0,
'menu_name' => $menu->id(),
));
return $this->entityManager->getForm($menu_link);
}
}
......@@ -188,7 +188,7 @@ function deleteCustomMenu($menu) {
$this->assertFalse($result, 'All menu links associated to the custom menu were deleted.');
// Make sure there's no delete button on system menus.
$this->drupalGet('admin/structure/menu/manage/main/edit');
$this->drupalGet('admin/structure/menu/manage/main');
$this->assertNoRaw('edit-delete', 'The delete button was not found');
// Try to delete the main menu.
......@@ -375,7 +375,7 @@ function testSystemMenuRename() {
$edit = array(
'label' => $this->randomName(16),
);
$this->drupalPost('admin/structure/menu/manage/main/edit', $edit, t('Save'));
$this->drupalPost('admin/structure/menu/manage/main', $edit, t('Save'));
// Make sure menu shows up with new name in block addition.
$default_theme = variable_get('theme_default', 'stark');
......
This diff is collapsed.
......@@ -68,16 +68,7 @@ function menu_menu() {
$items['admin/structure/menu'] = array(
'title' => 'Menus',
'description' => 'Add new menus to your site, edit existing menus, and rename and reorganize menu links.',
'page callback' => 'menu_overview_page',
'access callback' => 'user_access',
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
);
$items['admin/structure/menu/parents'] = array(
'title' => 'Parent menu items',
'page callback' => 'menu_parent_options_js',
'type' => MENU_CALLBACK,
'access arguments' => array('administer menu'),
'route_name' => 'menu_overview_page',
);
$items['admin/structure/menu/list'] = array(
'title' => 'List menus',
......@@ -85,40 +76,29 @@ function menu_menu() {
);
$items['admin/structure/menu/add'] = array(
'title' => 'Add menu',
'page callback' => 'menu_menu_add',
'access arguments' => array('administer menu'),
'route_name' => 'menu_menu_add',
'type' => MENU_LOCAL_ACTION,
'file' => 'menu.admin.inc',
);
$items['admin/structure/menu/settings'] = array(
'title' => 'Settings',
'route_name' => 'menu_settings',
'access arguments' => array('administer menu'),
'type' => MENU_LOCAL_TASK,
'weight' => 100,
);
$items['admin/structure/menu/manage/%menu'] = array(
'title' => 'Edit menu',
'page callback' => 'menu_menu_edit',
'page arguments' => array(4),
'route_name' => 'menu_menu_edit',
'title callback' => 'entity_page_label',
'title arguments' => array(4),
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
);
// Not officially a local action, but displayed as such in
// menu_overview_form().
$items['admin/structure/menu/manage/%menu/add'] = array(
'title' => 'Add menu link',
'page callback' => 'menu_link_add',
'page arguments' => array(4),
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
'title' => 'Add link',
'route_name' => 'menu_link_add',
'type' => MENU_LOCAL_ACTION,
);
$items['admin/structure/menu/manage/%menu/edit'] = array(
'title' => 'Edit menu',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['admin/structure/menu/manage/%menu/delete'] = array(
......@@ -127,9 +107,7 @@ function menu_menu() {
);
$items['admin/structure/menu/item/%menu_link/edit'] = array(
'title' => 'Edit menu link',
'page callback' => 'entity_get_form',
'page arguments' => array(4),
'access arguments' => array('administer menu'),
'route_name' => 'menu_link_edit',
);
$items['admin/structure/menu/item/%menu_link/reset'] = array(
'title' => 'Reset menu link',
......@@ -150,7 +128,8 @@ function menu_entity_info(&$entity_info) {
$entity_info['menu']['controllers']['access'] = 'Drupal\menu\MenuAccessController';
$entity_info['menu']['uri_callback'] = 'menu_uri';
$entity_info['menu']['controllers']['form'] = array(
'default' => 'Drupal\menu\MenuFormController',
'add' => 'Drupal\menu\MenuFormController',
'edit' => 'Drupal\menu\MenuFormController',
'delete' => 'Drupal\menu\Form\MenuDeleteForm',
);
......@@ -211,7 +190,7 @@ function menu_enable() {
$base_link = entity_create('menu_link', array(
'menu_name' => $system_link->menu_name,
'router_path' => 'admin/structure/menu/manage/%',
'router_path' => 'admin/structure/menu/manage/%menu',
'module' => 'menu',
));
......@@ -358,23 +337,6 @@ function menu_parent_options(array $menus, MenuLink $menu_link = NULL, $type = N
return _menu_get_options($menus, $available_menus, $menu_link);
}
/**
* Page callback.
* Get all the available menus and menu items as a JavaScript array.
*/
function menu_parent_options_js() {
$available_menus = array();
$menus = Drupal::request()->request->get('menus');
if (count($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);
}
/**
* Helper function to get the items of the given menu.
*/
......
......@@ -5,6 +5,34 @@ menu_settings:
requirements:
_permission: 'administer menu'
menu_overview_page:
pattern: '/admin/structure/menu'
defaults:
_entity_list: 'menu'
requirements:
_permission: 'administer menu'
menu_parent_options_js:
pattern: '/admin/structure/menu/parents'
defaults:
_controller: '\Drupal\menu\Controller\MenuController::getParentOptions'
requirements:
_permission: 'administer menu'
menu_link_add:
pattern: '/admin/structure/menu/manage/{menu}/add'
defaults:
_content: '\Drupal\menu\Controller\MenuController::addLink'
requirements:
_permission: 'administer menu'
menu_link_edit:
pattern: '/admin/structure/menu/item/{menu_link}/edit'
defaults:
_entity_form: 'menu_link'
requirements:
_permission: 'administer menu'
menu_link_reset:
pattern: 'admin/structure/menu/item/{menu_link}/reset'
defaults:
......@@ -19,9 +47,23 @@ menu_link_delete:
requirements:
_access_menu_delete_link: 'TRUE'
menu_menu_add:
pattern: '/admin/structure/menu/add'
defaults:
_entity_form: 'menu.add'
requirements:
_entity_create_access: 'menu'
menu_menu_edit:
pattern: '/admin/structure/menu/manage/{menu}'
defaults:
_entity_form: 'menu.edit'
requirements:
_entity_access: 'menu.update'
menu_delete_menu:
pattern: 'admin/structure/menu/manage/{menu}/delete'
defaults:
_entity_form: 'menu.delete'
requirements:
_access_menu_delete_menu: 'TRUE'
_entity_access: 'menu.delete'
......@@ -3,7 +3,3 @@ services:
class: Drupal\menu\Access\DeleteLinkAccessCheck
tags:
- { name: access_check }
access_check.menu.delete_menu:
class: Drupal\menu\Access\DeleteMenuAccessCheck
tags:
- { name: access_check }
......@@ -114,7 +114,6 @@ function testBreadCrumbs() {
'admin/structure/menu' => t('Menus'),
);
$this->assertBreadcrumb('admin/structure/menu/manage/tools', $trail);
$this->assertBreadcrumb('admin/structure/menu/manage/tools/edit', $trail);
$mlid_node_add = db_query('SELECT mlid FROM {menu_links} WHERE link_path = :href AND module = :module', array(
':href' => 'node/add',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment