4badcb6c66e5eda3c9e2eeba917d5b72e4431f16
[yaffs-website] / web / core / modules / link / src / Plugin / migrate / process / d6 / FieldLink.php
1 <?php
2
3 namespace Drupal\link\Plugin\migrate\process\d6;
4
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\migrate\Plugin\MigrationInterface;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\ProcessPluginBase;
9 use Drupal\migrate\Row;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * @MigrateProcessPlugin(
14  *   id = "d6_field_link"
15  * )
16  */
17 class FieldLink extends ProcessPluginBase implements ContainerFactoryPluginInterface {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
23     parent::__construct($configuration, $plugin_id, $plugin_definition);
24     $this->migration = $migration;
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
31     return new static(
32       $configuration,
33       $plugin_id,
34       $plugin_definition,
35       $migration
36     );
37   }
38
39   /**
40    * Turn a Drupal 6 URI into a Drupal 8-compatible format.
41    *
42    * @param string $uri
43    *   The 'url' value from Drupal 6.
44    *
45    * @return string
46    *   The Drupal 8-compatible URI.
47    *
48    * @see \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri()
49    */
50   protected function canonicalizeUri($uri) {
51     // If we already have a scheme, we're fine.
52     if (empty($uri) || !is_null(parse_url($uri, PHP_URL_SCHEME))) {
53       return $uri;
54     }
55
56     // Remove the <front> component of the URL.
57     if (strpos($uri, '<front>') === 0) {
58       $uri = substr($uri, strlen('<front>'));
59     }
60
61     // Add the internal: scheme and ensure a leading slash.
62     return 'internal:/' . ltrim($uri, '/');
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
69     $attributes = unserialize($value['attributes']);
70     // Drupal 6 link attributes might be double serialized.
71     if (!is_array($attributes)) {
72       $attributes = unserialize($attributes);
73     }
74
75     if (!$attributes) {
76       $attributes = [];
77     }
78
79     // Massage the values into the correct form for the link.
80     $route['uri'] = $this->canonicalizeUri($value['url']);
81     $route['options']['attributes'] = $attributes;
82     $route['title'] = $value['title'];
83     return $route;
84   }
85
86 }