function UnroutedUrlAssembler::addOptionDefaults

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php \Drupal\Core\Utility\UnroutedUrlAssembler::addOptionDefaults()
  2. 8.9.x core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php \Drupal\Core\Utility\UnroutedUrlAssembler::addOptionDefaults()
  3. 10 core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php \Drupal\Core\Utility\UnroutedUrlAssembler::addOptionDefaults()

Merges in default defaults.

Parameters

array $options: The options to merge in the defaults.

2 calls to UnroutedUrlAssembler::addOptionDefaults()
UnroutedUrlAssembler::buildExternalUrl in core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php
UnroutedUrlAssembler::buildLocalUrl in core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php

File

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

Class

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

Namespace

Drupal\Core\Utility

Code

protected function addOptionDefaults(array &$options) {
  $request = $this->requestStack
    ->getCurrentRequest();
  $current_base_path = $request->getBasePath() . '/';
  $current_script_path = '';
  $base_path_with_script = $request->getBaseUrl();
  // If the current request was made with the script name (eg, index.php) in
  // it, then extract it, making sure the leading / is gone, and a trailing /
  // is added, to allow simple string concatenation with other parts.
  if (!empty($base_path_with_script)) {
    $script_name = $request->getScriptName();
    if (str_contains($base_path_with_script, $script_name)) {
      $current_script_path = ltrim(substr($script_name, strlen($current_base_path)), '/') . '/';
    }
  }
  // Merge in defaults.
  $options += [
    'fragment' => '',
    'query' => [],
    'absolute' => FALSE,
    'prefix' => '',
    'script' => $current_script_path,
  ];
  if (isset($options['fragment']) && $options['fragment'] !== '') {
    $options['fragment'] = '#' . $options['fragment'];
  }
}

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