Loading core/modules/menu_link_content/menu_link_content.module +5 −0 Changes for core/modules/menu_link_content/menu_link_content.module: 5 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -104,6 +104,11 @@ function menu_link_content_entity_predelete(EntityInterface $entity) { $entity_type_id = $entity->getEntityTypeId(); foreach ($entity->uriRelationships() as $rel) { $url = $entity->toUrl($rel); // Entities can provide uri relationships that are not routed, in this case // getRouteParameters() will throw an exception. if (!$url->isRouted()) { continue; } $route_parameters = $url->getRouteParameters(); if (!isset($route_parameters[$entity_type_id])) { // Do not delete links which do not relate to this exact entity. For Loading core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php +10 −3 Changes for core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php: 10 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Menu\MenuTreeParameters; use Drupal\entity_test\Entity\EntityTestExternal; use Drupal\KernelTests\KernelTestBase; use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\system\Entity\Menu; Loading @@ -17,11 +18,10 @@ class MenuLinksTest extends KernelTestBase { /** * Modules to enable. * * @var array * {@inheritdoc} */ protected static $modules = [ 'entity_test', 'link', 'menu_link_content', 'router_test', Loading @@ -46,6 +46,7 @@ protected function setUp(): void { $this->installSchema('system', ['sequences']); $this->installSchema('user', ['users_data']); $this->installEntitySchema('entity_test_external'); $this->installEntitySchema('menu_link_content'); $this->installEntitySchema('user'); Loading Loading @@ -163,6 +164,12 @@ public function testMenuLinkOnEntityDelete() { $user = User::create(['name' => 'username']); $user->save(); // Create External test entity. $external_entity = EntityTestExternal::create(); $external_entity->save(); // Ensure an external entity can be deleted. $external_entity->delete(); // Create "canonical" menu link pointing to the user. $menu_link_content = MenuLinkContent::create([ 'title' => 'username profile', Loading core/modules/system/tests/modules/entity_test/src/Entity/EntityTestExternal.php 0 → 100644 +36 −0 Changes for core/modules/system/tests/modules/entity_test/src/Entity/EntityTestExternal.php: 36 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace Drupal\entity_test\Entity; use Drupal\Core\Url; /** * Test entity class. * * @ContentEntityType( * id = "entity_test_external", * label = @Translation("Entity test external"), * base_table = "entity_test_external", * entity_keys = { * "id" = "id", * "uuid" = "uuid", * "bundle" = "type", * }, * links = { * "canonical" = "/entity_test_external/{entity_test_external}" * }, * ) */ class EntityTestExternal extends EntityTest { /** * {@inheritdoc} */ public function toUrl($rel = 'canonical', array $options = []) { if ($rel === 'canonical') { return Url::fromUri('http://example.com', $options); } return parent::toUrl($rel, $options); } } Loading
core/modules/menu_link_content/menu_link_content.module +5 −0 Changes for core/modules/menu_link_content/menu_link_content.module: 5 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -104,6 +104,11 @@ function menu_link_content_entity_predelete(EntityInterface $entity) { $entity_type_id = $entity->getEntityTypeId(); foreach ($entity->uriRelationships() as $rel) { $url = $entity->toUrl($rel); // Entities can provide uri relationships that are not routed, in this case // getRouteParameters() will throw an exception. if (!$url->isRouted()) { continue; } $route_parameters = $url->getRouteParameters(); if (!isset($route_parameters[$entity_type_id])) { // Do not delete links which do not relate to this exact entity. For Loading
core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php +10 −3 Changes for core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php: 10 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Menu\MenuTreeParameters; use Drupal\entity_test\Entity\EntityTestExternal; use Drupal\KernelTests\KernelTestBase; use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\system\Entity\Menu; Loading @@ -17,11 +18,10 @@ class MenuLinksTest extends KernelTestBase { /** * Modules to enable. * * @var array * {@inheritdoc} */ protected static $modules = [ 'entity_test', 'link', 'menu_link_content', 'router_test', Loading @@ -46,6 +46,7 @@ protected function setUp(): void { $this->installSchema('system', ['sequences']); $this->installSchema('user', ['users_data']); $this->installEntitySchema('entity_test_external'); $this->installEntitySchema('menu_link_content'); $this->installEntitySchema('user'); Loading Loading @@ -163,6 +164,12 @@ public function testMenuLinkOnEntityDelete() { $user = User::create(['name' => 'username']); $user->save(); // Create External test entity. $external_entity = EntityTestExternal::create(); $external_entity->save(); // Ensure an external entity can be deleted. $external_entity->delete(); // Create "canonical" menu link pointing to the user. $menu_link_content = MenuLinkContent::create([ 'title' => 'username profile', Loading
core/modules/system/tests/modules/entity_test/src/Entity/EntityTestExternal.php 0 → 100644 +36 −0 Changes for core/modules/system/tests/modules/entity_test/src/Entity/EntityTestExternal.php: 36 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace Drupal\entity_test\Entity; use Drupal\Core\Url; /** * Test entity class. * * @ContentEntityType( * id = "entity_test_external", * label = @Translation("Entity test external"), * base_table = "entity_test_external", * entity_keys = { * "id" = "id", * "uuid" = "uuid", * "bundle" = "type", * }, * links = { * "canonical" = "/entity_test_external/{entity_test_external}" * }, * ) */ class EntityTestExternal extends EntityTest { /** * {@inheritdoc} */ public function toUrl($rel = 'canonical', array $options = []) { if ($rel === 'canonical') { return Url::fromUri('http://example.com', $options); } return parent::toUrl($rel, $options); } }