Commit e5493741 authored by catch's avatar catch

Issue #2350797 by pwolanin, amateescu, dawehner, dagmar: Orphaned menu links...

Issue #2350797 by pwolanin, amateescu, dawehner, dagmar: Orphaned menu links when nodes are deleted if menu_link_ui is not enabled
parent d44acb46
......@@ -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().
*/
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->toUrl($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();
}
}
}
}
}
......@@ -3,9 +3,11 @@
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\system\Entity\Menu;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
/**
* Tests handling of menu links hierarchies.
......@@ -140,6 +142,21 @@ public function testCreateLink() {
$this->assertEqual($link->getChangedTime(), REQUEST_TIME, 'Changing a menu link sets "changed" timestamp.');
}
/**
* 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', 'link' => [['uri' => 'entity:user/' . $user->id()]], 'bundle' => 'menu_test']);
$menu_link_content->save();
$menu_tree_condition = (new MenuTreeParameters())->addCondition('route_name', 'entity.user.canonical');
$this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
$user->delete();
$this->assertCount(0, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
}
/**
* Test automatic reparenting of menu links.
*/
......
......@@ -10,7 +10,6 @@
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Link;
use Drupal\Core\Menu\MenuLinkInterface;
......@@ -170,24 +169,6 @@ function _menu_ui_node_save(NodeInterface $node, array $values) {
$entity->save();
}
/**
* 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.
*
......
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