function FileUrlGenerator::generatePath

Same name in other branches
  1. 10 core/lib/Drupal/Core/File/FileUrlGenerator.php \Drupal\Core\File\FileUrlGenerator::generatePath()
  2. 11.x 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.

1 call to FileUrlGenerator::generatePath()
FileUrlGenerator::doGenerateString in core/lib/Drupal/Core/File/FileUrlGenerator.php
Creates an absolute web-accessible URL string.

File

core/lib/Drupal/Core/File/FileUrlGenerator.php, line 120

Class

FileUrlGenerator
Default implementation for the file URL generator service.

Namespace

Drupal\Core\File

Code

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;
    }
}

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