function NavigationLinkBlock::getUserEnteredStringAsUri

Gets the user-entered string as a URI.

The following two forms of input are mapped to URIs:

  • entity autocomplete ("label (entity id)") strings: to 'entity:' URIs;
  • strings without a detectable scheme: to 'internal:' URIs.

This method is the inverse of ::getUriAsDisplayableString().

Parameters

string $string: The user-entered string.

Return value

string The URI, if a non-empty $uri was passed.

See also

static::getUriAsDisplayableString()

1 call to NavigationLinkBlock::getUserEnteredStringAsUri()
NavigationLinkBlock::validateUriElement in core/modules/navigation/src/Plugin/Block/NavigationLinkBlock.php
Form element validation handler for the 'uri' element.

File

core/modules/navigation/src/Plugin/Block/NavigationLinkBlock.php, line 149

Class

NavigationLinkBlock
Defines a link navigation block.

Namespace

Drupal\navigation\Plugin\Block

Code

protected static function getUserEnteredStringAsUri($string) : string {
    // By default, assume the entered string is a URI.
    $uri = trim($string);
    // Detect entity autocomplete string, map to 'entity:' URI.
    $entity_id = EntityAutocomplete::extractEntityIdFromAutocompleteInput($string);
    if ($entity_id !== NULL) {
        // @todo Support entity types other than 'node'. Will be fixed in
        //   https://www.drupal.org/node/2423093.
        $uri = 'entity:node/' . $entity_id;
    }
    elseif (in_array($string, [
        '<nolink>',
        '<none>',
        '<button>',
    ], TRUE)) {
        $uri = 'route:' . $string;
    }
    elseif (!empty($string) && parse_url($string, PHP_URL_SCHEME) === NULL) {
        // @todo '<front>' is valid input for BC reasons, may be removed by
        //   https://www.drupal.org/node/2421941
        // - '<front>' -> '/'
        // - '<front>#foo' -> '/#foo'
        if (str_starts_with($string, '<front>')) {
            $string = '/' . substr($string, strlen('<front>'));
        }
        $uri = 'internal:' . $string;
    }
    return $uri;
}

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