ebb9a4d6fddd005bcfde287a05430b6a520ea268
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / Route.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Symfony\Component\DependencyInjection\ContainerInterface;
6 use Drupal\Core\Path\PathValidatorInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\migrate\MigrateExecutableInterface;
9 use Drupal\migrate\Plugin\MigrationInterface;
10 use Drupal\migrate\ProcessPluginBase;
11 use Drupal\migrate\Row;
12
13 /**
14  * Sets the destination route information based on the source link_path.
15  *
16  * The source value is an array of two values:
17  * - link_path: The path or URL of the route.
18  * - options: An array of URL options, e.g. query string, attributes, etc.
19  *
20  * Example:
21  *
22  * @code
23  * process:
24  *   new_route_field:
25  *     plugin: route
26  *     source:
27  *       - 'https://www.drupal.org'
28  *       -
29  *         attributes:
30  *           title: Drupal
31  * @endcode
32  *
33  * This will set new_route_field to be a route with the URL
34  * "https://www.drupal.org" and title attribute "Drupal".
35  *
36  * Example:
37  *
38  * @code
39  * process:
40  *   another_route_field:
41  *     plugin: route
42  *     source:
43  *       - 'user/login'
44  *       -
45  *         query:
46  *           destination: 'node/1'
47  * @endcode
48  *
49  * This will set another_route_field to be a route to the user login page
50  * (user/login) with a query string of "destination=node/1".
51  *
52  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
53  *
54  * @MigrateProcessPlugin(
55  *   id = "route"
56  * )
57  */
58 class Route extends ProcessPluginBase implements ContainerFactoryPluginInterface {
59
60   /**
61    * The path validator service.
62    *
63    * @var \Drupal\Core\Path\PathValidatorInterface
64    */
65   protected $pathValidator;
66
67   /**
68    * {@inheritdoc}
69    */
70   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, PathValidatorInterface $path_validator) {
71     parent::__construct($configuration, $plugin_id, $plugin_definition);
72     $this->migration = $migration;
73     $this->pathValidator = $path_validator;
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
80     return new static(
81       $configuration,
82       $plugin_id,
83       $plugin_definition,
84       $migration,
85       $container->get('path.validator')
86     );
87   }
88
89   /**
90    * {@inheritdoc}
91    *
92    * Set the destination route information based on the source link_path.
93    */
94   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
95     if (is_string($value)) {
96       $link_path = $value;
97       $options = [];
98     }
99     else {
100       list($link_path, $options) = $value;
101     }
102
103     $extracted = $this->pathValidator->getUrlIfValidWithoutAccessCheck($link_path);
104     $route = [];
105
106     if ($extracted) {
107       if ($extracted->isExternal()) {
108         $route['route_name'] = NULL;
109         $route['route_parameters'] = [];
110         $route['options'] = $options;
111         $route['url'] = $extracted->getUri();
112       }
113       else {
114         $route['route_name'] = $extracted->getRouteName();
115         $route['route_parameters'] = $extracted->getRouteParameters();
116         $route['options'] = $extracted->getOptions();
117
118         if (isset($options['query'])) {
119           // If the querystring is stored as a string (as in D6), convert it
120           // into an array.
121           if (is_string($options['query'])) {
122             parse_str($options['query'], $old_query);
123           }
124           else {
125             $old_query = $options['query'];
126           }
127           $options['query'] = $route['options']['query'] + $old_query;
128           unset($route['options']['query']);
129         }
130         $route['options'] = $route['options'] + $options;
131         $route['url'] = NULL;
132       }
133     }
134
135     return $route;
136   }
137
138 }