protected UrlGenerator::doGenerate(array $variables, array $defaults, array $tokens, array $parameters, array $query_params, $name)
Substitute the route parameters into the route path.
Note: This code was copied from \Symfony\Component\Routing\Generator\UrlGenerator::doGenerate() and shortened by removing code that is not relevant to Drupal or that is handled separately in ::generateFromRoute(). The Symfony version should be examined for changes in new Symfony releases.
Parameters
array $variables: The variables form the compiled route, corresponding to slugs in the route path.
array $defaults: The defaults from the route.
array $tokens: The tokens from the compiled route.
array $parameters: The route parameters passed to the generator. Parameters that do not match any variables will be added to the result as query parameters.
array $query_params: Query parameters passed to the generator as $options['query'].
string $name: The route name or other identifying string from ::getRouteDebugMessage().
Return value
string The url path, without any base path, including possible query string.
Throws
MissingMandatoryParametersException When some parameters are missing that are mandatory for the route.
InvalidParameterException When a parameter value for a placeholder is not correct because it does not match the requirement.
File
- core/lib/Drupal/Core/Routing/UrlGenerator.php, line 165
Class
- UrlGenerator
- Generates URLs from route names and parameters.
Namespace
Drupal\Core\Routing
Code
protected function doGenerate(array $variables, array $defaults, array $tokens, array $parameters, array $query_params, $name) { $variables = array_flip($variables); $mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters); // all params must be given if ($diff = array_diff_key($variables, $mergedParams)) { throw new MissingMandatoryParametersException(sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".', implode('", "', array_keys($diff)), $name)); } $url = ''; // Tokens start from the end of the path and work to the beginning. The // first one or several variable tokens may be optional, but once we find a // supplied token or a static text portion of the path, all remaining // variables up to the start of the path must be supplied to there is no gap. $optional = TRUE; // Structure of $tokens from the compiled route: // If the path is /admin/config/user-interface/shortcut/manage/{shortcut_set}/add-link-inline // [ [ 0 => 'text', 1 => '/add-link-inline' ], [ 0 => 'variable', 1 => '/', 2 => '[^/]++', 3 => 'shortcut_set' ], [ 0 => 'text', 1 => '/admin/config/user-interface/shortcut/manage' ] ] // // For a simple fixed path, there is just one token. // If the path is /admin/config // [ [ 0 => 'text', 1 => '/admin/config' ] ] foreach ($tokens as $token) { if ('variable' === $token[0]) { if (!$optional || !array_key_exists($token[3], $defaults) || (isset($mergedParams[$token[3]]) && (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]])) { // check requirement if (!preg_match('#^' . $token[2] . '$#', $mergedParams[$token[3]])) { $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]); throw new InvalidParameterException($message); } $url = $token[1] . $mergedParams[$token[3]] . $url; $optional = FALSE; } } else { // Static text $url = $token[1] . $url; $optional = FALSE; } } if ('' === $url) { $url = '/'; } // The contexts base URL is already encoded (see Symfony\Component\HttpFoundation\Request) $url = str_replace($this->decodedChars[0], $this->decodedChars[1], rawurlencode($url)); // Drupal paths rarely include dots, so skip this processing if possible. if (strpos($url, '/.') !== FALSE) { // the path segments "." and ".." are interpreted as relative reference when // resolving a URI; see http://tools.ietf.org/html/rfc3986#section-3.3 // so we need to encode them as they are not used for this purpose here // otherwise we would generate a URI that, when followed by a user agent // (e.g. browser), does not match this route $url = strtr($url, array('/../' => '/%2E%2E/', '/./' => '/%2E/')); if ('/..' === substr($url, -3)) { $url = substr($url, 0, -2) . '%2E%2E'; } elseif ('/.' === substr($url, -2)) { $url = substr($url, 0, -1) . '%2E'; } } // Add a query string if needed, including extra parameters. $query_params += array_diff_key($parameters, $variables, $defaults); if ($query_params && $query = UrlHelper::buildQuery($query_params)) { $url .= '?' . $query; } return $url; }
Please login to continue.