diff --git a/core/modules/menu_link_content/menu_link_content.module b/core/modules/menu_link_content/menu_link_content.module index 6d33dce..00d1eac 100644 --- a/core/modules/menu_link_content/menu_link_content.module +++ b/core/modules/menu_link_content/menu_link_content.module @@ -5,6 +5,7 @@ * Allows administrators to create custom menu links. */ +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\system\MenuInterface; @@ -36,3 +37,22 @@ function menu_link_content_menu_delete(MenuInterface $menu) { $menu_links = $storage->loadByProperties(array('menu_name' => $menu->id())); $storage->delete($menu_links); } + +/** + * Implements hook_entity_predelete() for node entities. + */ +function menu_link_content_entity_predelete(EntityInterface $entity) { + $entity_url = $entity->urlInfo(); + // Delete all MenuLinkContent links that point to this entity. + /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */ + $menu_link_manager = \Drupal::service('plugin.manager.menu.link'); + $result = $menu_link_manager->loadLinksByRoute($entity_url->getRouteName(), $entity_url->getRouteParameters()); + + if (!empty($result)) { + foreach ($result as $id => $instance) { + if ($instance->isDeletable() && strpos($id, 'menu_link_content:') === 0) { + $instance->deleteLink(); + } + } + } +} diff --git a/core/modules/menu_link_content/src/Tests/LinksTest.php b/core/modules/menu_link_content/src/Tests/LinksTest.php index 3cc8a95..f176c13 100644 --- a/core/modules/menu_link_content/src/Tests/LinksTest.php +++ b/core/modules/menu_link_content/src/Tests/LinksTest.php @@ -8,8 +8,12 @@ namespace Drupal\menu_link_content\Tests; use Drupal\Component\Utility\String; +use Drupal\Core\Menu\MenuTreeParameters; use Drupal\locale\TranslationString; +use Drupal\menu_link_content\Entity\MenuLinkContent; +use Drupal\node\Entity\Node; use Drupal\simpletest\WebTestBase; +use Drupal\user\Entity\User; /** * Tests handling of menu links hierarchies. @@ -145,6 +149,21 @@ public function testCreateLink() { } /** + * Tests that menu link pointing to entities get removed on entity remove. + */ + public function testMenuLinkOnEntityDelete() { + $user = User::create(['name' => 'username']); + $user->save(); + $menu_link_content = MenuLinkContent::create(['menu_name' => 'menu_test', 'route_name' => 'entity.user.canonical', 'route_parameters' => ['user' => $user->id()], 'bundle' => 'menu_test']); + $menu_link_content->save(); + $menu_tree_condition = (new MenuTreeParameters())->addCondition('route_name', 'entity.user.canonical'); + $this->assertEqual(count(\Drupal::menuTree()->load('menu_test', $menu_tree_condition)), 1); + + $user->delete(); + $this->assertEqual(count(\Drupal::menuTree()->load('menu_test', $menu_tree_condition)), 0); + } + + /** * Test automatic reparenting of menu links. */ function testMenuLinkReparenting($module = 'menu_test') { diff --git a/core/modules/menu_ui/menu_ui.module b/core/modules/menu_ui/menu_ui.module index 148203a..6f758ac 100644 --- a/core/modules/menu_ui/menu_ui.module +++ b/core/modules/menu_ui/menu_ui.module @@ -194,24 +194,6 @@ function menu_ui_node_save(EntityInterface $node) { } /** - * Implements hook_ENTITY_TYPE_predelete() for node entities. - */ -function menu_ui_node_predelete(EntityInterface $node) { - // Delete all MenuLinkContent links that point to this node. - /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */ - $menu_link_manager = \Drupal::service('plugin.manager.menu.link'); - $result = $menu_link_manager->loadLinksByRoute('entity.node.canonical', array('node' => $node->id())); - - if (!empty($result)) { - foreach ($result as $id => $instance) { - if ($instance->isDeletable() && strpos($id, 'menu_link_content:') === 0) { - $instance->deleteLink(); - } - } - } -} - -/** * Implements hook_node_prepare_form(). */ function menu_ui_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) {