protected function FileUrlGenerator::generatePath in Drupal 10
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/File/FileUrlGenerator.php \Drupal\Core\File\FileUrlGenerator::generatePath()
Generate a URL path.
Parameters
string $base_url: The base URL.
string $uri: The URI.
Return value
string The URL path.
File
- core/
lib/ Drupal/ Core/ File/ FileUrlGenerator.php, line 120
Class
- FileUrlGenerator
- Default implementation for the file URL generator service.
Namespace
Drupal\Core\FileCode
protected function generatePath(string $base_url, string $uri) : string {
// Allow for:
// - root-relative URIs (e.g. /foo.jpg in http://example.com/foo.jpg)
// - protocol-relative URIs (e.g. //bar.jpg, which is expanded to
// http://example.com/bar.jpg by the browser when viewing a page over
// HTTP and to https://example.com/bar.jpg when viewing a HTTPS page)
// Both types of relative URIs are characterized by a leading slash, hence
// we can use a single check.
if (mb_substr($uri, 0, 1) == '/') {
return $uri;
}
else {
// If this is not a properly formatted stream, then it is a shipped
// file. Therefore, return the urlencoded URI with the base URL
// prepended.
$options = UrlHelper::parse($uri);
$path = $base_url . UrlHelper::encodePath($options['path']);
// Append the query.
if ($options['query']) {
$path .= '?' . UrlHelper::buildQuery($options['query']);
}
// Append fragment.
if ($options['fragment']) {
$path .= '#' . $options['fragment'];
}
return $path;
}
}