protected function UnroutedUrlAssembler::buildLocalUrl in Drupal 9
Same name and namespace in other branches
- 8 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();
$uri = substr($uri, 5);
if (!empty($options['path_processing'])) {
$uri = $this->pathProcessor
->processOutbound($uri, $options, NULL, $generated_url);
}
$uri = ltrim($uri, '/');
$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;
}