Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / path / src / Plugin / migrate / destination / UrlAlias.php
1 <?php
2
3 namespace Drupal\path\Plugin\migrate\destination;
4
5 use Drupal\Core\Path\AliasStorage;
6 use Drupal\migrate\Plugin\MigrationInterface;
7 use Drupal\migrate\Row;
8 use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11
12 /**
13  * @MigrateDestination(
14  *   id = "url_alias"
15  * )
16  */
17 class UrlAlias extends DestinationBase implements ContainerFactoryPluginInterface {
18
19   /**
20    * The alias storage service.
21    *
22    * @var \Drupal\Core\Path\AliasStorage
23    */
24   protected $aliasStorage;
25
26   /**
27    * Constructs an entity destination plugin.
28    *
29    * @param array $configuration
30    *   A configuration array containing information about the plugin instance.
31    * @param string $plugin_id
32    *   The plugin_id for the plugin instance.
33    * @param mixed $plugin_definition
34    *   The plugin implementation definition.
35    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
36    *   The migration.
37    * @param \Drupal\Core\Path\AliasStorage $alias_storage
38    *   The alias storage service.
39    */
40   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, AliasStorage $alias_storage) {
41     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
42     $this->aliasStorage = $alias_storage;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
49     return new static(
50       $configuration,
51       $plugin_id,
52       $plugin_definition,
53       $migration,
54       $container->get('path.alias_storage')
55     );
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function import(Row $row, array $old_destination_id_values = []) {
62     $source = $row->getDestinationProperty('source');
63     $alias = $row->getDestinationProperty('alias');
64     $langcode = $row->getDestinationProperty('langcode');
65     $pid = $old_destination_id_values ? $old_destination_id_values[0] : NULL;
66
67     // Check if this alias is for a node and if that node is a translation.
68     if (preg_match('/^\/node\/\d+$/', $source) && $row->hasDestinationProperty('node_translation')) {
69
70       // Replace the alias source with the translation source path.
71       $node_translation = $row->getDestinationProperty('node_translation');
72       $source = '/node/' . $node_translation[0];
73       $langcode = $node_translation[1];
74     }
75
76     $path = $this->aliasStorage->save($source, $alias, $langcode, $pid);
77
78     return [$path['pid']];
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function getIds() {
85     $ids['pid']['type'] = 'integer';
86     return $ids;
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function fields(MigrationInterface $migration = NULL) {
93     return [
94       'pid' => 'The path id',
95       'source' => 'The source path.',
96       'alias' => 'The URL alias.',
97       'langcode' => 'The language code for the URL.',
98     ];
99   }
100
101 }