3 namespace Drupal\menu_link_content\Plugin\migrate\process;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\migrate\MigrateException;
9 use Drupal\migrate\MigrateExecutableInterface;
10 use Drupal\migrate\ProcessPluginBase;
11 use Drupal\migrate\Row;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Processes a link path into an 'internal:' or 'entity:' URI.
17 * @MigrateProcessPlugin(
21 class LinkUri extends ProcessPluginBase implements ContainerFactoryPluginInterface {
24 * The entity type manager, used to fetch entity link templates.
26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
28 protected $entityTypeManager;
31 * Constructs a LinkUri object.
33 * @param array $configuration
34 * A configuration array containing information about the plugin instance.
35 * @param string $plugin_id
36 * The plugin_id for the plugin instance.
37 * @param mixed $plugin_definition
38 * The plugin implementation definition.
39 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
40 * The entity type manager, used to fetch entity link templates.
42 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
43 parent::__construct($configuration, $plugin_id, $plugin_definition);
44 $this->entityTypeManager = $entity_type_manager;
50 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
55 $container->get('entity_type.manager')
62 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
64 $path = ltrim($path, '/');
66 if (parse_url($path, PHP_URL_SCHEME) === NULL) {
67 if ($path == '<front>') {
70 $path = 'internal:/' . $path;
72 // Convert entity URIs to the entity scheme, if the path matches a route
73 // of the form "entity.$entity_type_id.canonical".
74 // @see \Drupal\Core\Url::fromEntityUri()
75 $url = Url::fromUri($path);
76 if ($url->isRouted()) {
77 $route_name = $url->getRouteName();
78 foreach (array_keys($this->entityTypeManager->getDefinitions()) as $entity_type_id) {
79 if ($route_name == "entity.$entity_type_id.canonical" && isset($url->getRouteParameters()[$entity_type_id])) {
80 return "entity:$entity_type_id/" . $url->getRouteParameters()[$entity_type_id];
85 throw new MigrateException(sprintf('The path "%s" failed validation.', $path));