Commit e52e0fc4 authored by alexpott's avatar alexpott

Issue #1946428 by foopang | mtift: Convert all of confirm_form() in...

Issue #1946428 by foopang | mtift: Convert all of confirm_form() in menu.admin.inc to the new form interface.
parent 7f6cf775
<?php
/**
* @file
* Contains \Drupal\menu\Access\DeleteLinkAccessCheck.
*/
namespace Drupal\menu\Access;
use Drupal\Core\Access\AccessCheckInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Access check for menu link delete routes.
*/
class DeleteLinkAccessCheck implements AccessCheckInterface {
/**
* {@inheritdoc}
*/
public function applies(Route $route) {
return array_key_exists('_access_menu_delete_link', $route->getRequirements());
}
/**
* {@inheritdoc}
*/
public function access(Route $route, Request $request) {
if (user_access('administer menu') && $menu_link = $request->attributes->get('menu_link')) {
// Links defined via hook_menu may not be deleted. Updated items are an
// exception, as they can be broken.
return $menu_link->module !== 'system' || $menu_link->updated;
}
return FALSE;
}
}
<?php
/**
* @file
* Contains \Drupal\menu\Access\DeleteMenuAccessCheck.
*/
namespace Drupal\menu\Access;
use Drupal\Core\Access\AccessCheckInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Access check for menu delete routes.
*/
class DeleteMenuAccessCheck implements AccessCheckInterface {
/**
* {@inheritdoc}
*/
public function applies(Route $route) {
return array_key_exists('_access_menu_delete_menu', $route->getRequirements());
}
/**
* {@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\Form\MenuDeleteMenuForm.
*/
namespace Drupal\menu\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\system\Plugin\Core\Entity\Menu;
/**
* Defines a confirmation form for deletion of a custom menu.
*/
class MenuDeleteMenuForm extends ConfirmFormBase {
/**
* The menu object to be deleted.
*
* @var \Drupal\system\Plugin\Core\Entity\Menu
*/
protected $menu;
/**
* {@inheritdoc}
*/
protected function getQuestion() {
return t('Are you sure you want to delete the custom menu %title?', array('%title' => $this->menu->label()));
}
/**
* {@inheritdoc}
*/
protected function getCancelPath() {
return 'admin/structure/menu/manage/' . $this->menu->id();
}
/**
* {@inheritdoc}
*/
protected function getDescription() {
$caption = '';
$num_links = drupal_container()->get('plugin.manager.entity')
->getStorageController('menu_link')->countMenuLinks($this->menu->id());
if ($num_links) {
$caption .= '<p>' . format_plural($num_links, '<strong>Warning:</strong> There is currently 1 menu link in %title. It will be deleted (system-defined items will be reset).', '<strong>Warning:</strong> There are currently @count menu links in %title. They will be deleted (system-defined links will be reset).', array('%title' => $this->menu->label())) . '</p>';
}
$caption .= '<p>' . t('This action cannot be undone.') . '</p>';
return $caption;
}
/**
* {@inheritdoc}
*/
protected function getConfirmText() {
return t('Delete');
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'menu_delete_menu_confirm';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, Menu $menu = NULL) {
$this->menu = $menu;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$form_state['redirect'] = 'admin/structure/menu';
// System-defined menus may not be deleted - only menus defined by this module.
$system_menus = menu_list_system_menus();
if (isset($system_menus[$this->menu->id()])) {
return;
}
// Reset all the menu links defined by the system via hook_menu().
// @todo Convert this to an EFQ once we figure out 'ORDER BY m.number_parts'.
$result = db_query("SELECT mlid FROM {menu_links} ml INNER JOIN {menu_router} m ON ml.router_path = m.path WHERE ml.menu_name = :menu AND ml.module = 'system' ORDER BY m.number_parts ASC", array(':menu' => $this->menu->id()), array('fetch' => \PDO::FETCH_ASSOC))->fetchCol();
$menu_links = menu_link_load_multiple($result);
foreach ($menu_links as $link) {
$link->reset();
}
// Delete all links to the overview page for this menu.
$menu_links = entity_load_multiple_by_properties('menu_link', array('link_path' => 'admin/structure/menu/manage/' . $this->menu->id()));
menu_link_delete_multiple(array_keys($menu_links));
// Delete the custom menu and all its menu links.
$this->menu->delete();
$t_args = array('%title' => $this->menu->label());
drupal_set_message(t('The custom menu %title has been deleted.', $t_args));
watchdog('menu', 'Deleted custom menu %title and all its menu links.', $t_args, WATCHDOG_NOTICE);
}
}
<?php
/**
* @file
* Contains \Drupal\menu\Form\MenuLinkDeleteForm.
*/
namespace Drupal\menu\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\menu_link\Plugin\Core\Entity\MenuLink;
/**
* Defines a confirmation form for deletion of a single menu link.
*/
class MenuLinkDeleteForm extends ConfirmFormBase {
/**
* The menu link object to be deleted.
*
* @var \Drupal\menu_link\Plugin\Core\Entity\MenuLink
*/
protected $menuLink;
/**
* {@inheritdoc}
*/
protected function getQuestion() {
return t('Are you sure you want to delete the custom menu link %item?', array('%item' => $this->menuLink->link_title));
}
/**
* {@inheritdoc}
*/
protected function getCancelPath() {
return 'admin/structure/menu/manage/' . $this->menuLink->menu_name;
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'menu_link_delete_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, MenuLink $menu_link = NULL) {
$this->menuLink = $menu_link;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
menu_link_delete($this->menuLink->id());
$t_args = array('%title' => $this->menuLink->link_title);
drupal_set_message(t('The menu link %title has been deleted.', $t_args));
watchdog('menu', 'Deleted menu link %title.', $t_args, WATCHDOG_NOTICE);
$form_state['redirect'] = 'admin/structure/menu/manage/' . $this->menuLink->menu_name;
}
}
<?php
/**
* @file
* Contains \Drupal\menu\Form\MenuLinkResetForm.
*/
namespace Drupal\menu\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\menu_link\Plugin\Core\Entity\MenuLink;
/**
* Defines a confirmation form for resetting a single modified menu link.
*/
class MenuLinkResetForm extends ConfirmFormBase {
/**
* The menu link object to be deleted.
*
* @var \Drupal\menu_link\Plugin\Core\Entity\MenuLink
*/
protected $menuLink;
/**
* {@inheritdoc}
*/
protected function getQuestion() {
return t('Are you sure you want to reset the link %item to its default values?', array('%item' => $this->menuLink->link_title));
}
/**
* {@inheritdoc}
*/
protected function getCancelPath() {
return 'admin/structure/menu/manage/' . $this->menuLink->menu_name;
}
/**
* {@inheritdoc}
*/
protected function getDescription() {
return t('Any customizations will be lost. This action cannot be undone.');
}
/**
* {@inheritdoc}
*/
protected function getConfirmText() {
return t('Reset');
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'menu_link_reset_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, MenuLink $menu_link = NULL) {
$this->menuLink = $menu_link;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$new_menu_link = $this->menuLink->reset();
drupal_set_message(t('The menu link was reset to its default settings.'));
$form_state['redirect'] = 'admin/structure/menu/manage/' . $new_menu_link->menu_name;
}
}
......@@ -309,66 +309,6 @@ function theme_menu_overview_form($variables) {
return $output;
}
/**
* Menu callback; check access and get a confirm form for deletion of a custom menu.
*/
function menu_delete_menu_page(Menu $menu) {
// System-defined menus may not be deleted.
$system_menus = menu_list_system_menus();
if (isset($system_menus[$menu->id()])) {
throw new AccessDeniedHttpException();
}
return drupal_get_form('menu_delete_menu_confirm', $menu);
}
/**
* Build a confirm form for deletion of a custom menu.
*/
function menu_delete_menu_confirm($form, &$form_state, Menu $menu) {
$form['#menu'] = $menu;
$caption = '';
$num_links = drupal_container()->get('plugin.manager.entity')
->getStorageController('menu_link')->countMenuLinks($menu->id());
if ($num_links) {
$caption .= '<p>' . format_plural($num_links, '<strong>Warning:</strong> There is currently 1 menu link in %title. It will be deleted (system-defined items will be reset).', '<strong>Warning:</strong> There are currently @count menu links in %title. They will be deleted (system-defined links will be reset).', array('%title' => $menu->label())) . '</p>';
}
$caption .= '<p>' . t('This action cannot be undone.') . '</p>';
return confirm_form($form, t('Are you sure you want to delete the custom menu %title?', array('%title' => $menu->label())), 'admin/structure/menu/manage/' . $menu->id(), $caption, t('Delete'));
}
/**
* Delete a custom menu and all links in it.
*/
function menu_delete_menu_confirm_submit($form, &$form_state) {
$menu = $form['#menu'];
$form_state['redirect'] = 'admin/structure/menu';
// System-defined menus may not be deleted - only menus defined by this module.
$system_menus = menu_list_system_menus();
if (isset($system_menus[$menu->id()])) {
return;
}
// Reset all the menu links defined by the system via hook_menu().
// @todo Convert this to an EFQ once we figure out 'ORDER BY m.number_parts'.
$result = db_query("SELECT mlid FROM {menu_links} ml INNER JOIN {menu_router} m ON ml.router_path = m.path WHERE ml.menu_name = :menu AND ml.module = 'system' ORDER BY m.number_parts ASC", array(':menu' => $menu->id()), array('fetch' => PDO::FETCH_ASSOC))->fetchCol();
$menu_links = menu_link_load_multiple($result);
foreach ($menu_links as $link) {
$link->reset();
}
// Delete all links to the overview page for this menu.
$menu_links = entity_load_multiple_by_properties('menu_link', array('link_path' => 'admin/structure/menu/manage/' . $menu->id()));
menu_link_delete_multiple(array_keys($menu_links));
// Delete the custom menu and all its menu links.
$menu->delete();
$t_args = array('%title' => $menu->label());
drupal_set_message(t('The custom menu %title has been deleted.', $t_args));
watchdog('menu', 'Deleted custom menu %title and all its menu links.', $t_args, WATCHDOG_NOTICE);
}
/**
* Returns whether a menu name already exists.
*
......@@ -434,61 +374,3 @@ function menu_link_add(Menu $menu) {
drupal_set_title(t('Add menu link'));
return entity_get_form($menu_link);
}
/**
* Menu callback; Check access and present a confirm form for deleting a menu
* link.
*/
function menu_link_delete_page(MenuLink $menu_link) {
// Links defined via hook_menu may not be deleted. Updated items are an
// exception, as they can be broken.
if ($menu_link->module == 'system' && !$menu_link->updated) {
throw new AccessDeniedHttpException();
}
return drupal_get_form('menu_link_delete_form', $menu_link);
}
/**
* Build a confirm form for deletion of a single menu link.
*/
function menu_link_delete_form($form, &$form_state, MenuLink $menu_link) {
$form['#menu_link'] = $menu_link;
return confirm_form($form,
t('Are you sure you want to delete the custom menu link %item?', array('%item' => $menu_link->link_title)),
'admin/structure/menu/manage/' . $menu_link->menu_name
);
}
/**
* Processes menu link delete form submissions.
*/
function menu_link_delete_form_submit($form, &$form_state) {
$menu_link = $form['#menu_link'];
menu_link_delete($menu_link->id());
$t_args = array('%title' => $menu_link->link_title);
drupal_set_message(t('The menu link %title has been deleted.', $t_args));
watchdog('menu', 'Deleted menu link %title.', $t_args, WATCHDOG_NOTICE);
$form_state['redirect'] = 'admin/structure/menu/manage/' . $menu_link->menu_name;
}
/**
* Menu callback; Reset a single modified menu link.
*/
function menu_link_reset_form($form, &$form_state, MenuLink $menu_link) {
$form['#menu_link'] = $menu_link;
return confirm_form($form,
t('Are you sure you want to reset the link %item to its default values?', array('%item' => $menu_link->link_title)),
'admin/structure/menu/manage/' . $menu_link->menu_name,
t('Any customizations will be lost. This action cannot be undone.'),
t('Reset'));
}
/**
* Processes menu link reset form submissions.
*/
function menu_link_reset_form_submit($form, &$form_state) {
$menu_link = $form['#menu_link'];
$new_menu_link = $menu_link->reset();
drupal_set_message(t('The menu link was reset to its default settings.'));
$form_state['redirect'] = 'admin/structure/menu/manage/' . $new_menu_link->menu_name;
}
......@@ -123,10 +123,7 @@ function menu_menu() {
);
$items['admin/structure/menu/manage/%menu/delete'] = array(
'title' => 'Delete menu',
'page callback' => 'menu_delete_menu_page',
'page arguments' => array(4),
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
'route_name' => 'menu_delete_menu',
);
$items['admin/structure/menu/item/%menu_link/edit'] = array(
'title' => 'Edit menu link',
......@@ -136,17 +133,11 @@ function menu_menu() {
);
$items['admin/structure/menu/item/%menu_link/reset'] = array(
'title' => 'Reset menu link',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_link_reset_form', 4),
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
'route_name' => 'menu_link_reset',
);
$items['admin/structure/menu/item/%menu_link/delete'] = array(
'title' => 'Delete menu link',
'page callback' => 'menu_link_delete_page',
'page arguments' => array(4),
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
'route_name' => 'menu_link_delete',
);
return $items;
}
......
......@@ -4,3 +4,24 @@ menu_settings:
_form: 'Drupal\menu\MenuSettingsForm'
requirements:
_permission: 'administer menu'
menu_link_reset:
pattern: 'admin/structure/menu/item/{menu_link}/reset'
defaults:
_form: '\Drupal\menu\Form\MenuLinkResetForm'
requirements:
_permission: 'administer menu'
menu_link_delete:
pattern: 'admin/structure/menu/item/{menu_link}/delete'
defaults:
_form: '\Drupal\menu\Form\MenuLinkDeleteForm'
requirements:
_access_menu_delete_link: 'TRUE'
menu_delete_menu:
pattern: 'admin/structure/menu/manage/{menu}/delete'
defaults:
_form: '\Drupal\menu\Form\MenuDeleteMenuForm'
requirements:
_access_menu_delete_menu: 'TRUE'
services:
access_check.menu.delete_link:
class: Drupal\menu\Access\DeleteLinkAccessCheck
tags:
- { name: access_check }
access_check.menu.delete_menu:
class: Drupal\menu\Access\DeleteMenuAccessCheck
tags:
- { name: access_check }
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