protected function PathValidator::getUrl

protected PathValidator::getUrl($path, $access_check)

Helper for getUrlIfValid() and getUrlIfValidWithoutAccessCheck().

File

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

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

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Path!PathValidator.php/function/PathValidator::getUrl/8.1.x