3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Menu\MenuLinkManagerInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\migrate\Plugin\MigrationInterface;
10 use Drupal\migrate\MigrateExecutableInterface;
11 use Drupal\migrate\MigrateSkipRowException;
12 use Drupal\migrate\Plugin\MigrateProcessInterface;
13 use Drupal\migrate\ProcessPluginBase;
14 use Drupal\migrate\Row;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * This plugin figures out menu link parent plugin IDs.
20 * @MigrateProcessPlugin(
21 * id = "menu_link_parent"
24 class MenuLinkParent extends ProcessPluginBase implements ContainerFactoryPluginInterface {
27 * @var \Drupal\Core\Menu\MenuLinkManagerInterface
29 protected $menuLinkManager;
32 * @var \Drupal\migrate\Plugin\MigrateProcessInterface
34 protected $migrationPlugin;
37 * @var \Drupal\Core\Entity\EntityStorageInterface
39 protected $menuLinkStorage;
44 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrateProcessInterface $migration_plugin, MenuLinkManagerInterface $menu_link_manager, EntityStorageInterface $menu_link_storage) {
45 parent::__construct($configuration, $plugin_id, $plugin_definition);
46 $this->migrationPlugin = $migration_plugin;
47 $this->menuLinkManager = $menu_link_manager;
48 $this->menuLinkStorage = $menu_link_storage;
54 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
55 $migration_configuration['migration'][] = $migration->id();
60 $container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration),
61 $container->get('plugin.manager.menu.link'),
62 $container->get('entity.manager')->getStorage('menu_link_content')
69 * Find the parent link GUID.
71 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
72 $parent_id = array_shift($value);
78 $already_migrated_id = $this
80 ->transform($parent_id, $migrate_executable, $row, $destination_property);
81 if ($already_migrated_id && ($link = $this->menuLinkStorage->load($already_migrated_id))) {
82 return $link->getPluginId();
85 catch (MigrateSkipRowException $e) {
88 if (isset($value[1])) {
89 list($menu_name, $parent_link_path) = $value;
90 $url = Url::fromUserInput("/$parent_link_path");
91 if ($url->isRouted()) {
92 $links = $this->menuLinkManager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters(), $menu_name);
93 if (count($links) == 1) {
94 /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
95 $link = reset($links);
96 return $link->getPluginId();
100 throw new MigrateSkipRowException();