class FieldLink in Drupal 10
Same name and namespace in other branches
- 8 core/modules/link/src/Plugin/migrate/process/FieldLink.php \Drupal\link\Plugin\migrate\process\FieldLink
- 9 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 DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\link\Plugin\migrate\process\FieldLink
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FieldLink
1 file declares its use of FieldLink
- 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 the path starts with 2 slashes then it is always considered an
// external URL without an explicit protocol part.
// @todo Remove this when https://www.drupal.org/node/2744729 lands.
if (strpos($uri, '//') === 0) {
return $this->configuration['uri_scheme'] . ltrim($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
// cSpell:disable-next-line
$link_i_chars = '¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿŒœŸ';
// Pattern specific to internal links.
$internal_pattern = "/^(?:[a-z0-9" . $link_i_chars . "_\\-+\\[\\] ]+)";
$directories = "(?:\\/[a-z0-9" . $link_i_chars . "_\\-\\.~+%=&,\$'#!():;*@\\[\\]]*)*";
// Yes, four backslashes == a single backslash.
$query = "(?:\\/?\\?([?a-z0-9" . $link_i_chars . "+_|\\-\\.~\\/\\\\%=&,\$'():;*@\\[\\]{} ]*))";
$anchor = "(?:#[a-z0-9" . $link_i_chars . "_\\-\\.~+%=&,\$'():;*@\\[\\]\\/\\?]*)";
// 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_i_chars . "]|%[0-9a-f]{2})+(?::(?:[\\w" . $link_i_chars . "\\.\\-\\+%!\$&'\\(\\)*\\+,;=]|%[0-9a-f]{2})*)?)?@)";
$domain = '(?:(?:[a-z0-9' . $link_i_chars . ']([a-z0-9' . $link_i_chars . '\\-_\\[\\]])*)(\\.(([a-z0-9' . $link_i_chars . '\\-_\\[\\]])+\\.)*(' . $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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FieldLink:: |
protected | function | Turn a Drupal 6/7 URI into a Drupal 8-compatible format. | |
FieldLink:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |
|
FieldLink:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 18 |
MessengerTrait:: |
public | function | Gets the messenger. | 18 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function | ||
PluginBase:: |
public | function | ||
PluginBase:: |
public | function | 2 | |
PluginBase:: |
public | function | ||
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 1 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |