839b6396063b7885b70a4389327a1580c0e3a367
[yaffs-website] / web / core / modules / menu_link_content / src / Plugin / migrate / process / LinkUri.php
1 <?php
2
3 namespace Drupal\menu_link_content\Plugin\migrate\process;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\Core\Url;
8 use Drupal\migrate\MigrateExecutableInterface;
9 use Drupal\migrate\ProcessPluginBase;
10 use Drupal\migrate\Row;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Processes a link path into an 'internal:' or 'entity:' URI.
15  *
16  * @MigrateProcessPlugin(
17  *   id = "link_uri"
18  * )
19  */
20 class LinkUri extends ProcessPluginBase implements ContainerFactoryPluginInterface {
21
22   /**
23    * The entity type manager, used to fetch entity link templates.
24    *
25    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
26    */
27   protected $entityTypeManager;
28
29   /**
30    * Constructs a LinkUri object.
31    *
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.
40    */
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;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
50     return new static(
51       $configuration,
52       $plugin_id,
53       $plugin_definition,
54       $container->get('entity_type.manager')
55     );
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
62     list($path) = $value;
63     $path = ltrim($path, '/');
64
65     if (parse_url($path, PHP_URL_SCHEME) === NULL) {
66       if ($path == '<front>') {
67         $path = '';
68       }
69       $path = 'internal:/' . $path;
70
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];
80           }
81         }
82       }
83     }
84     return $path;
85   }
86
87 }