function UrlGenerator::doGenerate
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Routing/UrlGenerator.php \Drupal\Core\Routing\UrlGenerator::doGenerate()
- 10 core/lib/Drupal/Core/Routing/UrlGenerator.php \Drupal\Core\Routing\UrlGenerator::doGenerate()
- 11.x core/lib/Drupal/Core/Routing/UrlGenerator.php \Drupal\Core\Routing\UrlGenerator::doGenerate()
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 from 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']. This may be modified if there are extra parameters not used as route variables.
string $name: The route name or other identifying string from ::getRouteDebugMessage().
Return value
string The URL path, without any base path, without the query string, not URL encoded.
Throws
\Symfony\Component\Routing\Exception\MissingMandatoryParametersException When some parameters are missing that are mandatory for the route.
\Symfony\Component\Routing\Exception\InvalidParameterException When a parameter value for a placeholder is not correct because it does not match the requirement.
1 call to UrlGenerator::doGenerate()
- UrlGenerator::getInternalPathFromRoute in core/
lib/ Drupal/ Core/ Routing/ UrlGenerator.php - Gets the path of a route.
File
-
core/
lib/ Drupal/ Core/ Routing/ UrlGenerator.php, line 175
Class
- UrlGenerator
- Generates URLs from route names and parameters.
Namespace
Drupal\Core\RoutingCode
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 = '/';
}
// Add extra parameters to the query parameters.
$query_params += array_diff_key($parameters, $variables, $defaults);
return $url;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.