diff --git a/core/modules/menu_ui/menu_ui.routing.yml b/core/modules/menu_ui/menu_ui.routing.yml index 97fd744..24b6374 100644 --- a/core/modules/menu_ui/menu_ui.routing.yml +++ b/core/modules/menu_ui/menu_ui.routing.yml @@ -38,6 +38,19 @@ menu_ui.link_reset: _permission: 'administer menu' _custom_access: '\Drupal\menu_ui\Form\MenuLinkResetForm::linkIsResettable' +menu_ui.link_delete: + path: '/admin/structure/menu/link/{menu_link_plugin}/delete' + defaults: + _form: 'Drupal\menu_ui\Form\MenuLinkDeleteForm' + _title: 'Delete menu link' + options: + parameters: + menu_link_plugin: + type: menu_link_plugin + requirements: + _permission: 'administer menu' + _custom_access: '\Drupal\menu_ui\Form\MenuLinkDeleteForm::linkIsDeletable' + entity.menu.add_form: path: '/admin/structure/menu/add' defaults: diff --git a/core/modules/menu_ui/src/Form/MenuLinkDeleteForm.php b/core/modules/menu_ui/src/Form/MenuLinkDeleteForm.php new file mode 100644 index 0000000..9dc086e --- /dev/null +++ b/core/modules/menu_ui/src/Form/MenuLinkDeleteForm.php @@ -0,0 +1,128 @@ +menuLinkManager = $menu_link_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.menu.link') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'menu_link_delete_confirm'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return $this->t('Are you sure you want to delete the link %item?', array('%item' => $this->link->getTitle())); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + if ($this->link) { + return new Url('entity.menu.edit_form', array( + 'menu' => $this->link->getMenuName(), + )); + } + return new Url(''); + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return $this->t('The link will be deleted. This action cannot be undone.'); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, MenuLinkInterface $menu_link_plugin = NULL) { + $this->link = $menu_link_plugin; + + $form = parent::buildForm($form, $form_state); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->link = $this->menuLinkManager->removeDefinition($this->link->getPluginId()); + drupal_set_message($this->t('The menu link was deleted.')); + $form_state->setRedirectUrl($this->getCancelUrl()); + } + + /** + * Checks access based on whether the link can be deleted. + * + * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin + * The menu link plugin being checked. + * + * @return \Drupal\Core\Access\AccessResultInterface + * The access result. + */ + public function linkIsDeletable(MenuLinkInterface $menu_link_plugin) { + return AccessResult::allowedIf($menu_link_plugin->isDeletable())->setCacheable(FALSE); + } + +} diff --git a/core/modules/menu_ui/src/MenuForm.php b/core/modules/menu_ui/src/MenuForm.php index 1ad5271..1bbb425 100644 --- a/core/modules/menu_ui/src/MenuForm.php +++ b/core/modules/menu_ui/src/MenuForm.php @@ -396,8 +396,16 @@ protected function buildOverviewTreeForm($tree, $delta) { 'url' => Url::fromRoute('menu_ui.link_reset', ['menu_link_plugin' => $link->getPluginId()]), ); } - elseif ($delete_link = $link->getDeleteRoute()) { - $operations['delete']['url'] = $delete_link; + elseif ($link->isDeletable()) { + // Allow for a custom delete link per plugin. + $delete_route = $link->getDeleteRoute(); + if ($delete_route) { + $operations['delete']['url'] = $delete_route; + } + else { + // Fall back to the standard delete link. + $operations['delete']['url'] = Url::fromRoute('menu_ui.link_delete', ['menu_link_plugin' => $link->getPluginId()]); + } $operations['delete']['query']['destination'] = $this->entity->url(); $operations['delete']['title'] = $this->t('Delete'); }