diff --git a/core/modules/menu_link_content/menu_link_content.module b/core/modules/menu_link_content/menu_link_content.module index 0e02ba9..1b8b332 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; @@ -81,3 +82,24 @@ function menu_link_content_path_update($path) { function menu_link_content_path_delete($path) { _menu_link_content_update_path_alias($path['alias']); } + +/** + * Implements hook_entity_predelete() for node entities. + */ +function menu_link_content_entity_predelete(EntityInterface $entity) { + /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */ + $menu_link_manager = \Drupal::service('plugin.manager.menu.link'); + foreach ($entity->uriRelationships() as $rel) { + $url = $entity->urlInfo($rel); + // Delete all MenuLinkContent links that point to this entity route. + $result = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters()); + + if ($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/tests/src/Functional/LinksTest.php b/core/modules/menu_link_content/tests/src/Functional/LinksTest.php index 5a7121a..6f8886f 100644 --- a/core/modules/menu_link_content/tests/src/Functional/LinksTest.php +++ b/core/modules/menu_link_content/tests/src/Functional/LinksTest.php @@ -3,9 +3,12 @@ namespace Drupal\Tests\menu_link_content\Functional; use Drupal\Component\Utility\SafeMarkup; +use Drupal\Core\Menu\MenuTreeParameters; use Drupal\menu_link_content\Entity\MenuLinkContent; -use Drupal\Tests\BrowserTestBase; +use Drupal\node\Entity\Node; use Drupal\system\Entity\Menu; +use Drupal\Tests\BrowserTestBase; +use Drupal\user\Entity\User; /** * Tests handling of menu links hierarchies. @@ -141,6 +144,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. */ public function testMenuLinkReparenting($module = 'menu_test') { diff --git a/core/modules/menu_ui/menu_ui.module b/core/modules/menu_ui/menu_ui.module index cffccde..8533b30 100644 --- a/core/modules/menu_ui/menu_ui.module +++ b/core/modules/menu_ui/menu_ui.module @@ -167,24 +167,6 @@ function _menu_ui_node_save(NodeInterface $node, array $values) { } /** - * 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', ['node' => $node->id()]); - - if (!empty($result)) { - foreach ($result as $id => $instance) { - if ($instance->isDeletable() && strpos($id, 'menu_link_content:') === 0) { - $instance->deleteLink(); - } - } - } -} - -/** * Returns the definition for a menu link for the given node. * * @param \Drupal\node\NodeInterface $node