class FieldLink
Same name in this branch
- 8.9.x core/modules/link/src/Plugin/migrate/process/d6/FieldLink.php \Drupal\link\Plugin\migrate\process\d6\FieldLink
Same name in other branches
- 9 core/modules/link/src/Plugin/migrate/process/FieldLink.php \Drupal\link\Plugin\migrate\process\FieldLink
- 10 core/modules/link/src/Plugin/migrate/process/FieldLink.php \Drupal\link\Plugin\migrate\process\FieldLink
- 11.x core/modules/link/src/Plugin/migrate/process/FieldLink.php \Drupal\link\Plugin\migrate\process\FieldLink
Transform a pre-Drupal 8 formatted link for use in Drupal 8.
Previous to Drupal 8, URLs didn't need to have a URI scheme assigned. The contrib link module would auto-prefix the URL with a URI scheme. A link in Drupal 8 has more validation and external links must include the URI scheme. All external URIs need to be converted to use a URI scheme.
Available configuration keys
- uri_scheme: (optional) The URI scheme prefix to use for URLs without a scheme. Defaults to 'http://', which was the default in Drupal 6 and Drupal 7.
Examples:
Consider a link field migration, where you want to use https:// as the prefix:
process:
field_link:
plugin: field_link
uri_scheme: 'https://'
source: field_link
Plugin annotation
@MigrateProcessPlugin(
id = "field_link"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
- class \Drupal\migrate\ProcessPluginBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\migrate\Plugin\MigrateProcessInterface
- class \Drupal\link\Plugin\migrate\process\FieldLink extends \Drupal\migrate\ProcessPluginBase
- class \Drupal\migrate\ProcessPluginBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\migrate\Plugin\MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
Expanded class hierarchy of FieldLink
3 files declare their use of FieldLink
- CckLink.php in core/
modules/ link/ src/ Plugin/ migrate/ process/ d6/ CckLink.php - FieldLink.php in core/
modules/ link/ src/ Plugin/ migrate/ process/ d6/ FieldLink.php - FieldLinkTest.php in core/
modules/ link/ tests/ src/ Unit/ Plugin/ migrate/ process/ FieldLinkTest.php
File
-
core/
modules/ link/ src/ Plugin/ migrate/ process/ FieldLink.php, line 40
Namespace
Drupal\link\Plugin\migrate\processView source
class FieldLink extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
$configuration += [
'uri_scheme' => 'http://',
];
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* Turn a Drupal 6/7 URI into a Drupal 8-compatible format.
*
* @param string $uri
* The 'url' value from Drupal 6/7.
*
* @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 (parse_url($uri, PHP_URL_SCHEME)) {
return $uri;
}
// Empty URI and non-links are allowed.
if (empty($uri) || in_array($uri, [
'<nolink>',
'<none>',
])) {
return 'route:<nolink>';
}
// Remove the <front> component of the URL.
if (strpos($uri, '<front>') === 0) {
$uri = substr($uri, strlen('<front>'));
}
else {
// List of unicode-encoded characters that were allowed in URLs,
// according to link module in Drupal 7. Every character between ¿
// and ÿ (except × × and ÷ ÷) with the addition of
// Œ, œ and Ÿ.
// @see https://git.drupalcode.org/project/link/blob/7.x-1.5-beta2/link.module#L1382
$link_ichars = '¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿŒœŸ';
// Pattern specific to internal links.
$internal_pattern = "/^(?:[a-z0-9" . $link_ichars . "_\\-+\\[\\] ]+)";
$directories = "(?:\\/[a-z0-9" . $link_ichars . "_\\-\\.~+%=&,\$'#!():;*@\\[\\]]*)*";
// Yes, four backslashes == a single backslash.
$query = "(?:\\/?\\?([?a-z0-9" . $link_ichars . "+_|\\-\\.~\\/\\\\%=&,\$'():;*@\\[\\]{} ]*))";
$anchor = "(?:#[a-z0-9" . $link_ichars . "_\\-\\.~+%=&,\$'():;*@\\[\\]\\/\\?]*)";
// The rest of the path for a standard URL.
$end = $directories . '?' . $query . '?' . $anchor . '?$/i';
if (!preg_match($internal_pattern . $end, $uri)) {
$link_domains = '[a-z][a-z0-9-]{1,62}';
// Starting a parenthesis group with (?: means that it is grouped, but is not captured
$authentication = "(?:(?:(?:[\\w\\.\\-\\+!\$&'\\(\\)*\\+,;=" . $link_ichars . "]|%[0-9a-f]{2})+(?::(?:[\\w" . $link_ichars . "\\.\\-\\+%!\$&'\\(\\)*\\+,;=]|%[0-9a-f]{2})*)?)?@)";
$domain = '(?:(?:[a-z0-9' . $link_ichars . ']([a-z0-9' . $link_ichars . '\\-_\\[\\]])*)(\\.(([a-z0-9' . $link_ichars . '\\-_\\[\\]])+\\.)*(' . $link_domains . '|[a-z]{2}))?)';
$ipv4 = '(?:[0-9]{1,3}(\\.[0-9]{1,3}){3})';
$ipv6 = '(?:[0-9a-fA-F]{1,4}(\\:[0-9a-fA-F]{1,4}){7})';
$port = '(?::([0-9]{1,5}))';
// Pattern specific to external links.
$external_pattern = '/^' . $authentication . '?(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?';
if (preg_match($external_pattern . $end, $uri)) {
return $this->configuration['uri_scheme'] . $uri;
}
}
}
// 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/7 link attributes might be double serialized.
if (!is_array($attributes)) {
$attributes = unserialize($attributes);
}
// In rare cases Drupal 6/7 link attributes are triple serialized. To avoid
// further problems with them we set them to an empty array in this case.
if (!is_array($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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
FieldLink::canonicalizeUri | protected | function | Turn a Drupal 6/7 URI into a Drupal 8-compatible format. | ||
FieldLink::transform | public | function | Performs the associated process. | Overrides ProcessPluginBase::transform | |
FieldLink::__construct | public | function | |||
PluginInspectionInterface::getPluginDefinition | public | function | Gets the definition of the plugin implementation. | 6 | |
PluginInspectionInterface::getPluginId | public | function | Gets the plugin_id of the plugin instance. | 2 | |
ProcessPluginBase::multiple | public | function | Indicates whether the returned value requires multiple handling. | Overrides MigrateProcessInterface::multiple | 3 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.