You are here

public function TwigExtension::getUrl in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension::getUrl()

Generates an absolute URL given a route name and parameters.

@todo Add an option for scheme-relative URLs.

Parameters

$name: The name of the route.

array $parameters: 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 generated absolute URL for the given route.

File

core/lib/Drupal/Core/Template/TwigExtension.php, line 219

Class

TwigExtension
A class providing Drupal Twig extensions.

Namespace

Drupal\Core\Template

Code

public function getUrl($name, $parameters = [], $options = []) {
  assert($this->urlGenerator instanceof UrlGeneratorInterface, "The URL generator 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.");

  // Generate URL.
  $options['absolute'] = TRUE;
  $generated_url = $this->urlGenerator
    ->generateFromRoute($name, $parameters, $options, TRUE);

  // Return as render array, so we can bubble the bubbleable metadata.
  $build = [
    '#markup' => $generated_url
      ->getGeneratedUrl(),
  ];
  $generated_url
    ->applyTo($build);
  return $build;
}