function UnroutedUrlAssembler::buildExternalUrl

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php \Drupal\Core\Utility\UnroutedUrlAssembler::buildExternalUrl()
  2. 10 core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php \Drupal\Core\Utility\UnroutedUrlAssembler::buildExternalUrl()
  3. 11.x core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php \Drupal\Core\Utility\UnroutedUrlAssembler::buildExternalUrl()
1 call to UnroutedUrlAssembler::buildExternalUrl()
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 71

Class

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

Namespace

Drupal\Core\Utility

Code

protected function buildExternalUrl($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
    $this->addOptionDefaults($options);
    // Split off the query & fragment.
    $parsed = UrlHelper::parse($uri);
    $uri = $parsed['path'];
    $parsed += [
        'query' => [],
    ];
    $options += [
        'query' => [],
    ];
    $options['query'] = NestedArray::mergeDeepArray([
        $parsed['query'],
        $options['query'],
    ], TRUE);
    if ($parsed['fragment'] && !$options['fragment']) {
        $options['fragment'] = '#' . $parsed['fragment'];
    }
    if (isset($options['https'])) {
        if ($options['https'] === TRUE) {
            $uri = str_replace('http://', 'https://', $uri);
        }
        elseif ($options['https'] === FALSE) {
            $uri = str_replace('https://', 'http://', $uri);
        }
    }
    // Append the query.
    if ($options['query']) {
        $uri .= '?' . UrlHelper::buildQuery($options['query']);
    }
    // Reassemble.
    $url = $uri . $options['fragment'];
    return $collect_bubbleable_metadata ? (new GeneratedUrl())->setGeneratedUrl($url) : $url;
}

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