Unverified Commit 6029e8a9 authored by larowlan's avatar larowlan
Browse files

Issue #2939692 by Leksat, blazey, larowlan: menu_link_content_entity_predelete...

Issue #2939692 by Leksat, blazey, larowlan: menu_link_content_entity_predelete deletes "collection", "add-form", etc. links
parent 7e343df7
...@@ -89,10 +89,17 @@ function menu_link_content_path_delete($path) { ...@@ -89,10 +89,17 @@ function menu_link_content_path_delete($path) {
function menu_link_content_entity_predelete(EntityInterface $entity) { function menu_link_content_entity_predelete(EntityInterface $entity) {
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */ /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link'); $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$entity_type_id = $entity->getEntityTypeId();
foreach ($entity->uriRelationships() as $rel) { foreach ($entity->uriRelationships() as $rel) {
$url = $entity->toUrl($rel); $url = $entity->toUrl($rel);
$route_parameters = $url->getRouteParameters();
if (!isset($route_parameters[$entity_type_id])) {
// Do not delete links which do not relate to this exact entity. For
// example, "collection", "add-form", etc.
continue;
}
// Delete all MenuLinkContent links that point to this entity route. // Delete all MenuLinkContent links that point to this entity route.
$result = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters()); $result = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $route_parameters);
if ($result) { if ($result) {
foreach ($result as $id => $instance) { foreach ($result as $id => $instance) {
......
...@@ -147,8 +147,12 @@ public function testCreateLink() { ...@@ -147,8 +147,12 @@ public function testCreateLink() {
* Tests that menu link pointing to entities get removed on entity remove. * Tests that menu link pointing to entities get removed on entity remove.
*/ */
public function testMenuLinkOnEntityDelete() { public function testMenuLinkOnEntityDelete() {
// Create user.
$user = User::create(['name' => 'username']); $user = User::create(['name' => 'username']);
$user->save(); $user->save();
// Create "canonical" menu link pointing to the user.
$menu_link_content = MenuLinkContent::create([ $menu_link_content = MenuLinkContent::create([
'title' => 'username profile', 'title' => 'username profile',
'menu_name' => 'menu_test', 'menu_name' => 'menu_test',
...@@ -156,11 +160,30 @@ public function testMenuLinkOnEntityDelete() { ...@@ -156,11 +160,30 @@ public function testMenuLinkOnEntityDelete() {
'bundle' => 'menu_test', 'bundle' => 'menu_test',
]); ]);
$menu_link_content->save(); $menu_link_content->save();
// Create "collection" menu link pointing to the user listing page.
$menu_link_content_collection = MenuLinkContent::create([
'title' => 'users listing',
'menu_name' => 'menu_test',
'link' => [['uri' => 'internal:/' . $user->toUrl('collection')->getInternalPath()]],
'bundle' => 'menu_test',
]);
$menu_link_content_collection->save();
// Check is menu links present in the menu.
$menu_tree_condition = (new MenuTreeParameters())->addCondition('route_name', 'entity.user.canonical'); $menu_tree_condition = (new MenuTreeParameters())->addCondition('route_name', 'entity.user.canonical');
$this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition)); $this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
$menu_tree_condition_collection = (new MenuTreeParameters())->addCondition('route_name', 'entity.user.collection');
$this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition_collection));
// Delete the user.
$user->delete(); $user->delete();
// The "canonical" menu item has to be deleted.
$this->assertCount(0, \Drupal::menuTree()->load('menu_test', $menu_tree_condition)); $this->assertCount(0, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
// The "collection" menu item should still present in the menu.
$this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition_collection));
} }
/** /**
......
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