class FieldLink in Drupal 9
Same name and namespace in other branches
- 8 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 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_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
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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
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 |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
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. | 4 |
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. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |