class LinkGenerator implements LinkGeneratorInterface (View source)

Provides a class which generates a link with route names and parameters.

Properties

protected UrlGeneratorInterface $urlGenerator

The url generator.

protected ModuleHandlerInterface $moduleHandler

The module handler firing the route_link alter hook.

protected RendererInterface $renderer

The renderer service.

Methods

__construct(UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, RendererInterface $renderer)

Constructs a LinkGenerator instance.

generateFromLink(Link $link)

Renders a link from a link object.

generate(string|array|MarkupInterface $text, Url $url)

Renders a link to a URL.

GeneratedLink
doGenerate(GeneratedLink $generated_link, array $attributes, array $variables)

Generates the link.

Details

__construct(UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, RendererInterface $renderer)

Constructs a LinkGenerator instance.

Parameters

UrlGeneratorInterface $url_generator

The url generator.

ModuleHandlerInterface $module_handler

The module handler.

RendererInterface $renderer

The renderer service.

Renders a link from a link object.

Parameters

Link $link

A link object to convert to a string.

Return Value

GeneratedLink

A GeneratedLink object containing a link to the given route and parameters and bubbleable metadata.

GeneratedLink generate(string|array|MarkupInterface $text, Url $url)

Renders a link to a URL.

Examples:

Parameters

string|array|MarkupInterface $text

The link text for the anchor tag as a translated string or render array. Strings will be sanitized automatically. If you need to output HTML in the link text, use a render array or an already sanitized string such as the output of \Drupal\Component\Utility\Xss::filter() or \Drupal\Component\Render\FormattableMarkup.

Url $url

The URL object used for the link. Amongst its options, the following may be set to affect the generated link:

  • attributes: An associative array of HTML attributes to apply to the anchor tag. If element 'class' is included, it must be an array; 'title' must be a string; other elements are more flexible, as they just need to work as an argument for the constructor of the class Drupal\Core\Template\Attribute($options['attributes']).
  • language: An optional language object. If the path being linked to is internal to the site, $options['language'] is used to determine whether the link is "active", or pointing to the current page (the language as well as the path must match).
  • 'set_active_class': Whether this method should compare the $route_name, $parameters, language and query options to the current URL to determine whether the link is "active". Defaults to FALSE. If TRUE, an "active" class will be applied to the link. It is important to use this sparingly since it is usually unnecessary and requires extra processing.

Return Value

GeneratedLink

A GeneratedLink object containing a link to the given route and parameters and bubbleable metadata.

Exceptions

RouteNotFoundException
MissingMandatoryParametersException
InvalidParameterException

See also

system_page_attachments()

protected GeneratedLink doGenerate(GeneratedLink $generated_link, array $attributes, array $variables)

Generates the link.

Parameters

GeneratedLink $generated_link

The generated link, along with its associated cacheability metadata.

array $attributes

The attributes of the generated link.

array $variables

The link text, url, and other options.

Return Value

GeneratedLink

The generated link, along with its associated cacheability metadata.