function PathValidator::getUrl

Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Path/PathValidator.php \Drupal\Core\Path\PathValidator::getUrl()
  2. 10 core/lib/Drupal/Core/Path/PathValidator.php \Drupal\Core\Path\PathValidator::getUrl()
  3. 11.x core/lib/Drupal/Core/Path/PathValidator.php \Drupal\Core\Path\PathValidator::getUrl()

Helper for getUrlIfValid() and getUrlIfValidWithoutAccessCheck().

2 calls to PathValidator::getUrl()
PathValidator::getUrlIfValid in core/lib/Drupal/Core/Path/PathValidator.php
Returns a URL object, if the path is valid and accessible.
PathValidator::getUrlIfValidWithoutAccessCheck in core/lib/Drupal/Core/Path/PathValidator.php
Returns a URL object, if the path is valid.

File

core/lib/Drupal/Core/Path/PathValidator.php, line 95

Class

PathValidator
Provides a default path validator and access checker.

Namespace

Drupal\Core\Path

Code

protected function getUrl($path, $access_check) {
    $path = ltrim($path, '/');
    $parsed_url = UrlHelper::parse($path);
    $options = [];
    if (!empty($parsed_url['query'])) {
        $options['query'] = $parsed_url['query'];
    }
    if (!empty($parsed_url['fragment'])) {
        $options['fragment'] = $parsed_url['fragment'];
    }
    if ($parsed_url['path'] == '<front>') {
        return new Url('<front>', [], $options);
    }
    elseif ($parsed_url['path'] == '<none>') {
        return new Url('<none>', [], $options);
    }
    elseif (UrlHelper::isExternal($path) && UrlHelper::isValid($path)) {
        if (empty($parsed_url['path'])) {
            return FALSE;
        }
        return Url::fromUri($path);
    }
    $request = Request::create('/' . $path);
    $attributes = $this->getPathAttributes($path, $request, $access_check);
    if (!$attributes) {
        return FALSE;
    }
    $route_name = $attributes[RouteObjectInterface::ROUTE_NAME];
    $route_parameters = $attributes['_raw_variables']->all();
    return new Url($route_name, $route_parameters, $options + [
        'query' => $request->query
            ->all(),
    ]);
}

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