175566eee403e22b1fc1a223d819555e6d1fe3f9
[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\MigrateException;
9 use Drupal\migrate\MigrateExecutableInterface;
10 use Drupal\migrate\ProcessPluginBase;
11 use Drupal\migrate\Row;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Processes a link path into an 'internal:' or 'entity:' URI.
16  *
17  * @MigrateProcessPlugin(
18  *   id = "link_uri"
19  * )
20  */
21 class LinkUri extends ProcessPluginBase implements ContainerFactoryPluginInterface {
22
23   /**
24    * The entity type manager, used to fetch entity link templates.
25    *
26    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
27    */
28   protected $entityTypeManager;
29
30   /**
31    * Constructs a LinkUri object.
32    *
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.
41    */
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;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
51     return new static(
52       $configuration,
53       $plugin_id,
54       $plugin_definition,
55       $container->get('entity_type.manager')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
63     list($path) = $value;
64     $path = ltrim($path, '/');
65
66     if (parse_url($path, PHP_URL_SCHEME) === NULL) {
67       if ($path == '<front>') {
68         $path = '';
69       }
70       $path = 'internal:/' . $path;
71
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];
81           }
82         }
83       }
84       else {
85         throw new MigrateException(sprintf('The path "%s" failed validation.', $path));
86       }
87     }
88     return $path;
89   }
90
91 }