function UnroutedUrlAssembler::buildLocalUrl

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php \Drupal\Core\Utility\UnroutedUrlAssembler::buildLocalUrl()
  2. 10 core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php \Drupal\Core\Utility\UnroutedUrlAssembler::buildLocalUrl()
  3. 11.x core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php \Drupal\Core\Utility\UnroutedUrlAssembler::buildLocalUrl()
1 call to UnroutedUrlAssembler::buildLocalUrl()
UnroutedUrlAssembler::assemble in core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php
This is a helper function that calls buildExternalUrl() or buildLocalUrl() based on a check of whether the path is a valid external URL.

File

core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php, line 106

Class

UnroutedUrlAssembler
Provides a way to build external or non Drupal local domain URLs.

Namespace

Drupal\Core\Utility

Code

protected function buildLocalUrl($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
    $generated_url = $collect_bubbleable_metadata ? new GeneratedUrl() : NULL;
    $this->addOptionDefaults($options);
    $request = $this->requestStack
        ->getCurrentRequest();
    // Remove the base: scheme.
    // @todo Consider using a class constant for this in
    //   https://www.drupal.org/node/2417459
    $uri = substr($uri, 5);
    // Allow (outbound) path processing, if needed. A valid use case is the path
    // alias overview form:
    // @see \Drupal\path\Controller\PathController::adminOverview().
    if (!empty($options['path_processing'])) {
        // Do not pass the request, since this is a special case and we do not
        // want to include e.g. the request language in the processing.
        $uri = $this->pathProcessor
            ->processOutbound($uri, $options, NULL, $generated_url);
    }
    // Strip leading slashes from internal paths to prevent them becoming
    // external URLs without protocol. /example.com should not be turned into
    // //example.com.
    $uri = ltrim($uri, '/');
    // Add any subdirectory where Drupal is installed.
    $current_base_path = $request->getBasePath() . '/';
    if ($options['absolute']) {
        $current_base_url = $request->getSchemeAndHttpHost() . $current_base_path;
        if (isset($options['https'])) {
            if (!empty($options['https'])) {
                $base = str_replace('http://', 'https://', $current_base_url);
                $options['absolute'] = TRUE;
            }
            else {
                $base = str_replace('https://', 'http://', $current_base_url);
                $options['absolute'] = TRUE;
            }
        }
        else {
            $base = $current_base_url;
        }
        if ($collect_bubbleable_metadata) {
            $generated_url->addCacheContexts([
                'url.site',
            ]);
        }
    }
    else {
        $base = $current_base_path;
    }
    $prefix = empty($uri) ? rtrim($options['prefix'], '/') : $options['prefix'];
    $uri = str_replace('%2F', '/', rawurlencode($prefix . $uri));
    $query = $options['query'] ? '?' . UrlHelper::buildQuery($options['query']) : '';
    $url = $base . $options['script'] . $uri . $query . $options['fragment'];
    return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
}

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