You are here

protected function PathValidator::getUrl in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Path/PathValidator.php \Drupal\Core\Path\PathValidator::getUrl()
  2. 9 core/lib/Drupal/Core/Path/PathValidator.php \Drupal\Core\Path\PathValidator::getUrl()

Helper for getUrlIfValid() and getUrlIfValidWithoutAccessCheck().

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(),
  ]);
}