function FieldLink::canonicalizeUri

Same name in other branches
  1. 9 core/modules/link/src/Plugin/migrate/process/FieldLink.php \Drupal\link\Plugin\migrate\process\FieldLink::canonicalizeUri()
  2. 8.9.x core/modules/link/src/Plugin/migrate/process/FieldLink.php \Drupal\link\Plugin\migrate\process\FieldLink::canonicalizeUri()
  3. 11.x core/modules/link/src/Plugin/migrate/process/FieldLink.php \Drupal\link\Plugin\migrate\process\FieldLink::canonicalizeUri()

Turn a Drupal 6/7 URI into a Drupal 8-compatible format.

Parameters

string $uri: The 'url' value from Drupal 6/7.

Return value

string The Drupal 8-compatible URI.

See also

\Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri()

1 call to FieldLink::canonicalizeUri()
FieldLink::transform in core/modules/link/src/Plugin/migrate/process/FieldLink.php
Performs the associated process.

File

core/modules/link/src/Plugin/migrate/process/FieldLink.php, line 59

Class

FieldLink
Transform a pre-Drupal 8 formatted link for use in Drupal 8.

Namespace

Drupal\link\Plugin\migrate\process

Code

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 (str_starts_with($uri, '//')) {
        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 (str_starts_with($uri, '<front>')) {
        $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_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, '/');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.