Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / MenuLinkParent.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Menu\MenuLinkManagerInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\Core\Url;
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;
16
17 /**
18  * This plugin figures out menu link parent plugin IDs.
19  *
20  * @MigrateProcessPlugin(
21  *   id = "menu_link_parent"
22  * )
23  */
24 class MenuLinkParent extends ProcessPluginBase implements ContainerFactoryPluginInterface {
25
26   /**
27    * @var \Drupal\Core\Menu\MenuLinkManagerInterface
28    */
29   protected $menuLinkManager;
30
31   /**
32    * @var \Drupal\migrate\Plugin\MigrateProcessInterface
33    */
34   protected $migrationPlugin;
35
36   /**
37    * @var \Drupal\Core\Entity\EntityStorageInterface
38    */
39   protected $menuLinkStorage;
40
41   /**
42    * {@inheritdoc}
43    */
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;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
55     $migration_configuration['migration'][] = $migration->id();
56     return new static(
57       $configuration,
58       $plugin_id,
59       $plugin_definition,
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')
63     );
64   }
65
66   /**
67    * {@inheritdoc}
68    *
69    * Find the parent link GUID.
70    */
71   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
72     $parent_id = array_shift($value);
73     if (!$parent_id) {
74       // Top level item.
75       return '';
76     }
77     try {
78       $already_migrated_id = $this
79         ->migrationPlugin
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();
83       }
84     }
85     catch (MigrateSkipRowException $e) {
86
87     }
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();
97         }
98       }
99     }
100     throw new MigrateSkipRowException();
101   }
102
103 }