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\MigrateExecutableInterface;
9 use Drupal\migrate\ProcessPluginBase;
10 use Drupal\migrate\Row;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Processes a link path into an 'internal:' or 'entity:' URI.
16 * @MigrateProcessPlugin(
20 class LinkUri extends ProcessPluginBase implements ContainerFactoryPluginInterface {
23 * The entity type manager, used to fetch entity link templates.
25 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
27 protected $entityTypeManager;
30 * Constructs a LinkUri object.
32 * @param array $configuration
33 * A configuration array containing information about the plugin instance.
34 * @param string $plugin_id
35 * The plugin_id for the plugin instance.
36 * @param mixed $plugin_definition
37 * The plugin implementation definition.
38 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
39 * The entity type manager, used to fetch entity link templates.
41 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
42 parent::__construct($configuration, $plugin_id, $plugin_definition);
43 $this->entityTypeManager = $entity_type_manager;
49 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
54 $container->get('entity_type.manager')
61 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
63 $path = ltrim($path, '/');
65 if (parse_url($path, PHP_URL_SCHEME) === NULL) {
66 if ($path == '<front>') {
69 $path = 'internal:/' . $path;
71 // Convert entity URIs to the entity scheme, if the path matches a route
72 // of the form "entity.$entity_type_id.canonical".
73 // @see \Drupal\Core\Url::fromEntityUri()
74 $url = Url::fromUri($path);
75 if ($url->isRouted()) {
76 $route_name = $url->getRouteName();
77 foreach (array_keys($this->entityTypeManager->getDefinitions()) as $entity_type_id) {
78 if ($route_name == "entity.$entity_type_id.canonical" && isset($url->getRouteParameters()[$entity_type_id])) {
79 return "entity:$entity_type_id/" . $url->getRouteParameters()[$entity_type_id];