function FileUrlGenerator::doGenerateString

Same name in other branches
  1. 9 core/lib/Drupal/Core/File/FileUrlGenerator.php \Drupal\Core\File\FileUrlGenerator::doGenerateString()
  2. 10 core/lib/Drupal/Core/File/FileUrlGenerator.php \Drupal\Core\File\FileUrlGenerator::doGenerateString()

Creates an absolute web-accessible URL string.

Parameters

string $uri: The URI to a file for which we need an external URL, or the path to a shipped file.

bool $relative: Whether to return an relative or absolute URL.

Return value

string An absolute string containing a URL that may be used to access the file.

Throws

\Drupal\Core\File\Exception\InvalidStreamWrapperException If a stream wrapper could not be found to generate an external URL.

2 calls to FileUrlGenerator::doGenerateString()
FileUrlGenerator::generateAbsoluteString in core/lib/Drupal/Core/File/FileUrlGenerator.php
Creates an absolute web-accessible URL string.
FileUrlGenerator::generateString in core/lib/Drupal/Core/File/FileUrlGenerator.php
Creates a root-relative web-accessible URL string.

File

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

Class

FileUrlGenerator
Default implementation for the file URL generator service.

Namespace

Drupal\Core\File

Code

protected function doGenerateString(string $uri, bool $relative) : string {
    // Allow the URI to be altered, e.g. to serve a file from a CDN or static
    // file server.
    $this->moduleHandler
        ->alter('file_url', $uri);
    $scheme = StreamWrapperManager::getScheme($uri);
    if (!$scheme) {
        $baseUrl = $relative ? base_path() : $this->requestStack
            ->getCurrentRequest()
            ->getSchemeAndHttpHost() . base_path();
        return $this->generatePath($baseUrl, $uri);
    }
    elseif ($scheme == 'http' || $scheme == 'https' || $scheme == 'data') {
        // Check for HTTP and data URI-encoded URLs so that we don't have to
        // implement getExternalUrl() for the HTTP and data schemes.
        return $relative ? $this->transformRelative($uri) : $uri;
    }
    elseif ($wrapper = $this->streamWrapperManager
        ->getViaUri($uri)) {
        // Attempt to return an external URL using the appropriate wrapper.
        $externalUrl = $wrapper->getExternalUrl();
        return $relative ? $this->transformRelative($externalUrl) : $externalUrl;
    }
    throw new InvalidStreamWrapperException();
}

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