diff -u b/core/modules/link/src/Plugin/migrate/process/FieldLink.php b/core/modules/link/src/Plugin/migrate/process/FieldLink.php --- b/core/modules/link/src/Plugin/migrate/process/FieldLink.php +++ b/core/modules/link/src/Plugin/migrate/process/FieldLink.php @@ -20,6 +20,7 @@ * {@inheritdoc} */ public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) { + $configuration += ['uri_scheme' => 'http://']; parent::__construct($configuration, $plugin_id, $plugin_definition); $this->migration = $migration; } @@ -49,7 +50,7 @@ */ protected function canonicalizeUri($uri) { // If we already have a scheme, we're fine. - if (empty($uri) || !is_null(parse_url($uri, PHP_URL_SCHEME))) { + if (empty($uri) || !is_null(parse_url($uri, \PHP_URL_SCHEME))) { return $uri; } @@ -87,12 +88,7 @@ // Pattern specific to external links. $external_pattern = '/^' . $protocol . '?' . $authentication . '?(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?'; if (preg_match($external_pattern . $end, $uri)) { - if (!empty($this->configuration['url_scheme']) && ($this->configuration['url_scheme'] === "https://")) { - return $this->configuration['url_scheme'] . $uri; - } - else { - return 'http://' . $uri; - } + return $this->configuration['uri_scheme'] . $uri; } } } diff -u /dev/null b/core/modules/link/src/Plugin/migrate/process/d6/FieldLink.php --- /dev/null +++ b/core/modules/link/src/Plugin/migrate/process/d6/FieldLink.php @@ -0,0 +1,91 @@ +migration = $migration; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration + ); + } + + /** + * Turn a Drupal 6 URI into a Drupal 8-compatible format. + * + * @param string $uri + * The 'url' value from Drupal 6. + * + * @return string + * The Drupal 8-compatible URI. + * + * @see \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri() + */ + protected function canonicalizeUri($uri) { + // If we already have a scheme, we're fine. + if (empty($uri) || !is_null(parse_url($uri, PHP_URL_SCHEME))) { + return $uri; + } + + // Remove the component of the URL. + if (strpos($uri, '') === 0) { + $uri = substr($uri, strlen('')); + } + + // Add the internal: scheme and ensure a leading slash. + return 'internal:/' . ltrim($uri, '/'); + } + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + $attributes = unserialize($value['attributes']); + // Drupal 6 link attributes might be double serialized. + if (!is_array($attributes)) { + $attributes = unserialize($attributes); + } + + if (!$attributes) { + $attributes = []; + } + + // Massage the values into the correct form for the link. + $route['uri'] = $this->canonicalizeUri($value['url']); + $route['options']['attributes'] = $attributes; + $route['title'] = $value['title']; + return $route; + } + +} diff -u b/core/modules/link/tests/src/Unit/Plugin/migrate/process/d6/FieldLinkTest.php b/core/modules/link/tests/src/Unit/Plugin/migrate/process/d6/FieldLinkTest.php --- b/core/modules/link/tests/src/Unit/Plugin/migrate/process/d6/FieldLinkTest.php +++ b/core/modules/link/tests/src/Unit/Plugin/migrate/process/d6/FieldLinkTest.php @@ -2,11 +2,12 @@ namespace Drupal\Tests\link\Unit\Plugin\migrate\process\d6; -use Drupal\link\Plugin\migrate\process\FieldLink; +use Drupal\link\Plugin\migrate\process\d6\FieldLink; use Drupal\Tests\UnitTestCase; /** * @group Link + * @group legacy */ class FieldLinkTest extends UnitTestCase { @@ -50,22 +51,6 @@ 'scheme:test', 'scheme:test', ], - 'Absolute URL with protocol prefix' => [ - 'http://www.google.com', - 'http://www.google.com', - ], - 'Absolute URL without protocol prefix' => [ - 'www.yahoo.com', - 'http://www.yahoo.com', - ], - 'Absolute URL without protocol prefix nor www' => [ - 'yahoo.com', - 'http://yahoo.com', - ], - 'Absolute URL with non-standard characters' => [ - 'http://www.ßÀÑÐ¥ƒå¢ë.com', - 'http://www.ßÀÑÐ¥ƒå¢ë.com', - ], ]; } only in patch2: unchanged: --- a/core/modules/link/src/Plugin/migrate/field/d7/LinkField.php +++ b/core/modules/link/src/Plugin/migrate/field/d7/LinkField.php @@ -45,4 +45,15 @@ public function processFieldInstance(MigrationInterface $migration) { $migration->mergeProcessOfProperty('settings/title', $process); } + /** + * {@inheritdoc} + */ + public function processFieldValues(MigrationInterface $migration, $field_name, $data) { + $process = [ + 'plugin' => 'field_link', + 'source' => $field_name, + ]; + $migration->mergeProcessOfProperty($field_name, $process); + } + } only in patch2: unchanged: --- a/core/modules/link/src/Plugin/migrate/process/d6/CckLink.php +++ b/core/modules/link/src/Plugin/migrate/process/d6/CckLink.php @@ -2,9 +2,9 @@ namespace Drupal\link\Plugin\migrate\process\d6; -@trigger_error('CckLink is deprecated in Drupal 8.3.x and will be removed before -Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\process\d6\FieldLink instead.', -E_USER_DEPRECATED); +use Drupal\link\Plugin\migrate\process\FieldLink; + +@trigger_error('CckLink is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\process\FieldLink instead.', E_USER_DEPRECATED); /** * @MigrateProcessPlugin( @@ -12,6 +12,6 @@ * ) * * @deprecated in Drupal 8.3.x, to be removed before Drupal 9.0.x. Use - * \Drupal\link\Plugin\migrate\process\d6\FieldLink instead. + * \Drupal\link\Plugin\migrate\process\FieldLink instead. */ class CckLink extends FieldLink {} only in patch2: unchanged: --- /dev/null +++ b/core/modules/link/tests/src/Unit/Plugin/migrate/process/FieldLinkTest.php @@ -0,0 +1,84 @@ +getMock(MigrationInterface::class)); + $transformed = $link_plugin->transform([ + 'url' => $url, + 'title' => '', + 'attributes' => serialize([]), + ], $this->getMock(MigrateExecutableInterface::class), $this->getMockBuilder(Row::class)->disableOriginalConstructor()->getMock(), NULL); + $this->assertEquals($expected, $transformed['uri']); + } + + /** + * Data provider for testCanonicalizeUri. + */ + public function canonicalizeUriDataProvider() { + return [ + 'Simple front-page' => [ + '', + 'internal:/', + [], + ], + 'Front page with query' => [ + '?query=1', + 'internal:/?query=1', + [], + ], + 'No leading forward slash' => [ + 'node/10', + 'internal:/node/10', + [], + ], + 'Leading forward slash' => [ + '/node/10', + 'internal:/node/10', + [], + ], + 'Existing scheme' => [ + 'scheme:test', + 'scheme:test', + [], + ], + 'Absolute URL with protocol prefix' => [ + 'http://www.google.com', + 'http://www.google.com', + [], + ], + 'Absolute URL without protocol prefix' => [ + 'www.yahoo.com', + 'http://www.yahoo.com', + [], + ], + 'Absolute URL without protocol prefix nor www' => [ + 'yahoo.com', + 'https://yahoo.com', + ['uri_scheme' => 'https://'], + ], + 'Absolute URL with non-standard characters' => [ + 'http://www.ßÀÑÐ¥ƒå¢ë.com', + 'http://www.ßÀÑÐ¥ƒå¢ë.com', + [], + ], + ]; + } + +}