View source
<?php
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;
use Drupal\Core\Url;
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuLinkParent extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $menuLinkManager;
protected $migrationPlugin;
protected $migration;
protected $migrateLookup;
protected $menuLinkStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, $migrate_lookup, MenuLinkManagerInterface $menu_link_manager, EntityStorageInterface $menu_link_storage, MigrationInterface $migration = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
if ($migrate_lookup instanceof MigrateProcessInterface) {
@trigger_error('Passing a migration process plugin as the fourth argument to ' . __METHOD__ . ' is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268', E_USER_DEPRECATED);
$this->migrationPlugin = $migrate_lookup;
$migrate_lookup = \Drupal::service('migrate.lookup');
}
elseif (!$migrate_lookup instanceof MigrateLookupInterface) {
throw new \InvalidArgumentException("The fourth argument to " . __METHOD__ . " must be an instance of MigrateLookupInterface.");
}
elseif (!$migration instanceof MigrationInterface) {
throw new \InvalidArgumentException("The seventh argument to " . __METHOD__ . " must be an instance of MigrationInterface.");
}
$this->migration = $migration;
$this->migrateLookup = $migrate_lookup;
$this->menuLinkManager = $menu_link_manager;
$this->menuLinkStorage = $menu_link_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$migration_configuration['migration'][] = $migration
->id();
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('migrate.lookup'), $container
->get('plugin.manager.menu.link'), $container
->get('entity_type.manager')
->getStorage('menu_link_content'), $migration);
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$parent_id = array_shift($value);
if (!$parent_id) {
return '';
}
if ($this->migrationPlugin) {
try {
$already_migrated_id = $this->migrationPlugin
->transform($parent_id, $migrate_executable, $row, $destination_property);
} catch (MigrateSkipRowException $e) {
}
}
else {
$lookup_result = $this->migrateLookup
->lookup($this->migration
->id(), [
$parent_id,
]);
if ($lookup_result) {
$already_migrated_id = $lookup_result[0]['id'];
}
}
if (!empty($already_migrated_id) && ($link = $this->menuLinkStorage
->load($already_migrated_id))) {
return $link
->getPluginId();
}
if (isset($value[1])) {
list($menu_name, $parent_link_path) = $value;
$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) {
$link = reset($links);
return $link
->getPluginId();
}
}
throw new MigrateSkipRowException(sprintf("No parent link found for plid '%d' in menu '%s'.", $parent_id, $value[0]));
}
}