UnroutedUrlAssembler::buildLocalUrl

protected UnroutedUrlAssembler::buildLocalUrl($uri, array $options = [], $collect_bubbleable_metadata = FALSE)

File

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

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;
}
doc_Drupal
2016-10-29 09:50:28
Comments
Leave a Comment

Please login to continue.