You are here

class FieldLink in Drupal 9

Same name and namespace in other branches
  1. 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

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\process
View 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 &#x00BF;
      // and &#x00FF; (except × &#x00D7; and ÷ &#x00F7;) with the addition of
      // &#x0152;, &#x0153; and &#x0178;.
      // @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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
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 Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.