diff --git a/core/modules/menu_link_content/menu_link_content.module b/core/modules/menu_link_content/menu_link_content.module index def0ed9db0e728ba1ea910d042e520b89b3cb7e8..a593bef4e3c986e376e6fb8b58f66a0faad087aa 100644 --- a/core/modules/menu_link_content/menu_link_content.module +++ b/core/modules/menu_link_content/menu_link_content.module @@ -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 diff --git a/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php b/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php index d7d3812f70c38094d800804513a15d5a91ff6e2c..6883c76abbe0d07397a49829dcdbf81defb686c9 100644 --- a/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php +++ b/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php @@ -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; @@ -17,11 +18,10 @@ class MenuLinksTest extends KernelTestBase { /** - * Modules to enable. - * - * @var array + * {@inheritdoc} */ - public static $modules = [ + protected static $modules = [ + 'entity_test', 'link', 'menu_link_content', 'router_test', @@ -46,6 +46,7 @@ protected function setUp() { $this->installSchema('system', ['sequences']); $this->installSchema('user', ['users_data']); + $this->installEntitySchema('entity_test_external'); $this->installEntitySchema('menu_link_content'); $this->installEntitySchema('user'); @@ -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', diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestExternal.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestExternal.php new file mode 100644 index 0000000000000000000000000000000000000000..b6587a9a9e151ffb41a84d3575054015ec8cc62f --- /dev/null +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestExternal.php @@ -0,0 +1,36 @@ +<?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); + } + +}