Skip to content
Snippets Groups Projects
Unverified Commit 296ba5d8 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2968170 by joelpittet, idebr, heddn, quietone, mikelutz, tim.plunkett:...

Issue #2968170 by joelpittet, idebr, heddn, quietone, mikelutz, tim.plunkett: MenuLinkParent breaks migration when Parent URI is external
parent 3954b06d
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
......@@ -85,17 +86,25 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
catch (MigrateSkipRowException $e) {
}
if (isset($value[1])) {
list($menu_name, $parent_link_path) = $value;
$url = Url::fromUserInput("/$parent_link_path");
if ($url->isRouted()) {
$links = $this->menuLinkManager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters(), $menu_name);
if (count($links) == 1) {
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$link = reset($links);
return $link->getPluginId();
$links = [];
if (UrlHelper::isExternal($parent_link_path)) {
$links = $this->menuLinkStorage->loadByProperties(['link__uri' => $parent_link_path]);
}
else {
$url = Url::fromUserInput("/$parent_link_path");
if ($url->isRouted()) {
$links = $this->menuLinkManager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters(), $menu_name);
}
}
if (count($links) == 1) {
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$link = reset($links);
return $link->getPluginId();
}
}
throw new MigrateSkipRowException(sprintf("No parent link found for plid '%d' in menu '%s'.", $parent_id, $value[0]));
}
......
......@@ -2,8 +2,10 @@
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\menu_link_content\MenuLinkContentInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\migrate\process\MenuLinkParent;
use Drupal\migrate\Plugin\MigrateProcessInterface;
......@@ -35,4 +37,28 @@ public function testTransformException() {
$this->plugin->transform([1, 'admin', NULL], $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Tests the plugin when the parent is an external link.
*
* @covers ::transform
*/
public function testTransformExternal() {
$migration_plugin = $this->prophesize(MigrateProcessInterface::class);
$menu_link_manager = $this->prophesize(MenuLinkManagerInterface::class);
$menu_link_storage = $this->prophesize(EntityStorageInterface::class);
$menu_link_content = $this->prophesize(MenuLinkContentInterface::class);
$menu_link_content->getPluginId()->willReturn('menu_link_content:fe151460-dfa2-4133-8864-c1746f28ab27');
$menu_link_storage->loadByProperties([
'link__uri' => 'http://example.com',
])->willReturn([
9054 => $menu_link_content,
]);
$plugin = $this->prophesize(PluginInspectionInterface::class);
$menu_link_manager->createInstance('menu_link_content:fe151460-dfa2-4133-8864-c1746f28ab27')->willReturn($plugin->reveal());
$plugin = new MenuLinkParent([], 'map', [], $migration_plugin->reveal(), $menu_link_manager->reveal(), $menu_link_storage->reveal());
$result = $plugin->transform([1, 'admin', 'http://example.com'], $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertEquals('menu_link_content:fe151460-dfa2-4133-8864-c1746f28ab27', $result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment