public function HelpTwigExtension::getRouteLink in Drupal 9
Returns a link or plain text, given text, route name, and parameters.
Parameters
string $text: The link text.
string $route: The name of the route.
array $parameters: (optional) An associative array of route parameter names and values.
array $options: (optional) An associative array of additional options. The 'absolute' option is forced to be TRUE.
Return value
array A render array with a generated absolute link to the given route. If the user does not have permission for the route, or an exception occurs, such as a missing route or missing parameters, the render array is for the link text as a plain string instead.
See also
\Drupal\Core\Template\TwigExtension::getUrl()
1 call to HelpTwigExtension::getRouteLink()
- HelpTwigExtension::getTopicLink in core/
modules/ help_topics/ src/ HelpTwigExtension.php - Returns a link to a help topic, or the title of the topic.
File
- core/
modules/ help_topics/ src/ HelpTwigExtension.php, line 85
Class
- HelpTwigExtension
- Defines and registers Drupal Twig extensions for rendering help topics.
Namespace
Drupal\help_topicsCode
public function getRouteLink(string $text, string $route, array $parameters = [], array $options = []) : array {
assert($this->accessManager instanceof AccessManagerInterface, "The access manager hasn't been set up. Any configuration YAML file with a service directive dealing with the Twig configuration can cause this, most likely found in a recently installed or changed module.");
$bubbles = new BubbleableMetadata();
$bubbles
->addCacheTags([
'route_match',
]);
try {
$access_object = $this->accessManager
->checkNamedRoute($route, $parameters, NULL, TRUE);
$bubbles
->addCacheableDependency($access_object);
if ($access_object
->isAllowed()) {
$options['absolute'] = TRUE;
$url = Url::fromRoute($route, $parameters, $options);
// Generate the URL to check for parameter problems and collect
// cache metadata.
$generated = $url
->toString(TRUE);
$bubbles
->addCacheableDependency($generated);
$build = [
'#title' => $text,
'#type' => 'link',
'#url' => $url,
];
}
else {
// If the user doesn't have access, return the link text.
$build = [
'#markup' => $text,
];
}
} catch (RouteNotFoundException|MissingMandatoryParametersException|InvalidParameterException $e) {
// If the route had one of these exceptions, return the link text.
$build = [
'#markup' => $text,
];
}
$bubbles
->applyTo($build);
return $build;
}