Implements \Drupal\Core\Routing\PathBasedGeneratorInterface::generateFromPath().
$path: (optional) The internal path or external URL being linked to, such as "node/34" or "http://example.com/foo". The default value is equivalent to passing in '<front>'. A few notes:
$options: (optional) An associative array of additional options, with the following elements:
A string containing a URL to the given path.
\Drupal\Core\Routing\GeneratorNotInitializedException.
Overrides PathBasedGeneratorInterface::generateFromPath
public function generateFromPath($path = NULL, $options = array()) {
if (!$this
->initialized()) {
throw new GeneratorNotInitializedException();
}
// Merge in defaults.
$options += array(
'fragment' => '',
'query' => array(),
'absolute' => FALSE,
'prefix' => '',
);
if (!isset($options['external'])) {
// Return an external link if $path contains an allowed absolute URL. Only
// call the slow drupal_strip_dangerous_protocols() if $path contains a ':'
// before any / ? or #. Note: we could use url_is_external($path) here, but
// that would require another function call, and performance inside url() is
// critical.
$colonpos = strpos($path, ':');
$options['external'] = $colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && UrlValidator::stripDangerousProtocols($path) == $path;
}
if (isset($options['fragment']) && $options['fragment'] !== '') {
$options['fragment'] = '#' . $options['fragment'];
}
if ($options['external']) {
// Split off the fragment.
if (strpos($path, '#') !== FALSE) {
list($path, $old_fragment) = explode('#', $path, 2);
// If $options contains no fragment, take it over from the path.
if (isset($old_fragment) && !$options['fragment']) {
$options['fragment'] = '#' . $old_fragment;
}
}
// Append the query.
if ($options['query']) {
$path .= (strpos($path, '?') !== FALSE ? '&' : '?') . $this
->httpBuildQuery($options['query']);
}
if (isset($options['https']) && $this->mixedModeSessions) {
if ($options['https'] === TRUE) {
$path = str_replace('http://', 'https://', $path);
}
elseif ($options['https'] === FALSE) {
$path = str_replace('https://', 'http://', $path);
}
}
// Reassemble.
return $path . $options['fragment'];
}
else {
$path = ltrim($this
->processPath($path, $options), '/');
}
if (!isset($options['script'])) {
$options['script'] = $this->scriptPath;
}
// The base_url might be rewritten from the language rewrite in domain mode.
if (!isset($options['base_url'])) {
if (isset($options['https']) && $this->mixedModeSessions) {
if ($options['https'] === TRUE) {
$options['base_url'] = str_replace('http://', 'https://', $this->baseUrl);
$options['absolute'] = TRUE;
}
elseif ($options['https'] === FALSE) {
$options['base_url'] = str_replace('https://', 'http://', $this->baseUrl);
$options['absolute'] = TRUE;
}
}
else {
$options['base_url'] = $this->baseUrl;
}
}
elseif (rtrim($options['base_url'], '/') == $options['base_url']) {
$options['base_url'] .= '/';
}
$base = $options['absolute'] ? $options['base_url'] : $this->basePath;
$prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];
$path = str_replace('%2F', '/', rawurlencode($prefix . $path));
$query = $options['query'] ? '?' . $this
->httpBuildQuery($options['query']) : '';
return $base . $options['script'] . $path . $query . $options['fragment'];
}