diff --git a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php index 49235b928d271a5c2df572c7075005d9f5fc4c7f..129404407bbee3cf8d27b14479d5e070e1ec3d87 100644 --- a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php +++ b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php @@ -235,6 +235,14 @@ public static function preDelete(EntityStorageInterface $storage, array $entitie foreach ($entities as $menu_link) { /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link */ $menu_link_manager->removeDefinition($menu_link->getPluginId(), FALSE); + + // Children get re-attached to the menu link's parent. + $parent_plugin_id = $menu_link->getParentId(); + $children = $storage->loadByProperties(['parent' => $menu_link->getPluginId()]); + foreach ($children as $child) { + /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $child */ + $child->set('parent', $parent_plugin_id)->save(); + } } } diff --git a/core/modules/menu_link_content/tests/src/Kernel/MenuLinkContentDeleteTest.php b/core/modules/menu_link_content/tests/src/Kernel/MenuLinkContentDeleteTest.php new file mode 100644 index 0000000000000000000000000000000000000000..98dead915cda29973e115e44952d6eea4796ef7f --- /dev/null +++ b/core/modules/menu_link_content/tests/src/Kernel/MenuLinkContentDeleteTest.php @@ -0,0 +1,62 @@ +<?php + +namespace Drupal\Tests\menu_link_content\Kernel; + +use Drupal\menu_link_content\Entity\MenuLinkContent; +use Drupal\KernelTests\KernelTestBase; + +/** + * Tests the menu link content delete function. + * + * @group menu_link_content + */ +class MenuLinkContentDeleteTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['menu_link_content', 'link', 'system']; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + $this->installEntitySchema('menu_link_content'); + } + + /** + * Tests the MenuLinkContent::preDelete function. + */ + public function testMenuLinkContentDelete() { + // Add new menu items in a hierarchy. + $parent = MenuLinkContent::create([ + 'title' => $this->randomMachineName(8), + 'link' => [['uri' => 'internal:/']], + 'menu_name' => 'main', + ]); + $parent->save(); + $child1 = MenuLinkContent::create([ + 'title' => $this->randomMachineName(8), + 'link' => [['uri' => 'internal:/']], + 'menu_name' => 'main', + 'parent' => 'menu_link_content:' . $parent->uuid(), + ]); + $child1->save(); + $child2 = MenuLinkContent::create([ + 'title' => $this->randomMachineName(8), + 'link' => [['uri' => 'internal:/']], + 'menu_name' => 'main', + 'parent' => 'menu_link_content:' . $child1->uuid(), + ]); + $child2->save(); + + // Delete the middle child. + $child1->delete(); + // Refresh $child2. + $child2 = MenuLinkContent::load($child2->id()); + // Test the reference in the child. + $this->assertSame('menu_link_content:' . $parent->uuid(), $child2->getParentId()); + } + +}