Skip to content

Commit

Permalink
Issue #2914785 by acbramley, jungle, Hardik_Patel_12, mrinalini9, lar…
Browse files Browse the repository at this point in the history
…owlan: Entities with external urls as a uri relationship can not be deleted when menu_link_content is installed
  • Loading branch information
larowlan committed Jul 7, 2020
1 parent 69937f4 commit 9a14906
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
5 changes: 5 additions & 0 deletions modules/menu_link_content/menu_link_content.module
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -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');

Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 9a14906

Please sign in to comment.