protected static Url::fromInternalUri(array $uri_parts, array $options)
Creates a new Url object for 'internal:' URIs.
Important note: the URI minus the scheme can NOT simply be validated by a \Drupal\Core\Path\PathValidatorInterface implementation. The semantics of the 'internal:' URI scheme are different:
To clarify, a few examples of path plus corresponding 'internal:' URI:
Therefore, when using a PathValidatorInterface to validate 'internal:' URIs, we must map:
array $uri_parts: Parts from an URI of the form internal:{path} as from parse_url().
array $options: An array of options, see \Drupal\Core\Url::fromUri() for details.
\Drupal\Core\Url A new Url object for a 'internal:' URI.
\InvalidArgumentException Thrown when the URI's path component doesn't have a leading slash.
protected static function fromInternalUri(array $uri_parts, array $options) { // Both PathValidator::getUrlIfValidWithoutAccessCheck() and 'base:' URIs // only accept/contain paths without a leading slash, unlike 'internal:' // URIs, for which the leading slash means "relative to Drupal root" and // "relative to Symfony app root" (just like in Symfony/Drupal 8 routes). if (empty($uri_parts['path'])) { $uri_parts['path'] = '<none>'; } elseif ($uri_parts['path'] === '/') { $uri_parts['path'] = '<front>'; } else { if ($uri_parts['path'][0] !== '/') { throw new \InvalidArgumentException("The internal path component '{$uri_parts['path']}' is invalid. Its path component must have a leading slash, e.g. internal:/foo."); } // Remove the leading slash. $uri_parts['path'] = substr($uri_parts['path'], 1); if (UrlHelper::isExternal($uri_parts['path'])) { throw new \InvalidArgumentException("The internal path component '{$uri_parts['path']}' is external. You are not allowed to specify an external URL together with internal:/."); } } $url = \Drupal::pathValidator() ->getUrlIfValidWithoutAccessCheck($uri_parts['path']) ? : static::fromUri('base:' . $uri_parts['path'], $options); // Allow specifying additional options. $url->setOptions($options + $url->getOptions()); return $url; }
© 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!Url.php/function/Url::fromInternalUri/8.1.x