Drupal 8  8.0.2
LinkGenerator Class Reference
Inheritance diagram for LinkGenerator:

Public Member Functions

 __construct (UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, RendererInterface $renderer)
 generateFromLink (Link $link)
 generate ($text, Url $url)

Protected Attributes


Detailed Description

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

Constructor & Destructor Documentation

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

Constructs a LinkGenerator instance.

\Drupal\Core\Routing\UrlGeneratorInterface$url_generatorThe url generator.
\Drupal\Core\Extension\ModuleHandlerInterface$module_handlerThe module handler.
\Drupal\Core\Render\RendererInterface$rendererThe renderer service.

References Drupal\moduleHandler(), and Drupal\urlGenerator().

Here is the call graph for this function:

Member Function Documentation

generate (   $text,
Url  $url 

{Renders a link to a URL.Examples:

$link_generator = \Drupal::service('link_generator');
$installer_url = \Drupal\Core\Url::fromUri('base://core/install.php');
$installer_link = $link_generator->generate($text, $installer_url);
$external_url = \Drupal\Core\Url::fromUri('http://example.com', ['query' => ['foo' => 'bar']]);
$external_link = $link_generator->generate($text, $external_url);
$internal_url = \Drupal\Core\Url::fromRoute('system.admin');
$internal_link = $link_generator->generate($text, $internal_url);

However, for links enclosed in translatable text you should use t() and embed the HTML anchor tag directly in the translated string. For example:

$text = t('Visit the <a href=":url">content types</a> page', array(':url' => \Drupal::url('entity.node_type.collection')));

This keeps the context of the link title ('settings' in the example) for translators.

string | array$textThe 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 ::filter() or ::format().
\Drupal\Core\Url$urlThe 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($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.
A GeneratedLink object containing a link to the given route and parameters and bubbleable metadata.
\Symfony\Component\Routing\Exception\RouteNotFoundExceptionThrown when the named route doesn't exist.
\Symfony\Component\Routing\Exception\MissingMandatoryParametersExceptionThrown when some parameters are missing that are mandatory for the route.
\Symfony\Component\Routing\Exception\InvalidParameterExceptionThrown when a parameter value for a placeholder is not correct because it does not match the requirement.

For anonymous users, the "active" class will be calculated on the server, because most sites serve each anonymous user the same cached page anyway. For authenticated users, the "active" class will be calculated on the client (through JavaScript), only data- attributes are added to links to prevent breaking the render cache. The JavaScript is added in system_page_attachments().

See Also

Implements LinkGeneratorInterface.

References BubbleableMetadata\createFromObject(), Json\encode(), Html\escape(), Url\getInternalPath(), Url\getOptions(), Url\isExternal(), Url\isRouted(), SafeMarkup\isSafe(), Drupal\moduleHandler(), Url\setOptions(), Url\setUrlGenerator(), Url\toString(), and Drupal\urlGenerator().

Referenced by LinkGenerator\generateFromLink().

Here is the call graph for this function:

Here is the caller graph for this function:

generateFromLink ( Link  $link)

{Renders a link from a link object.

\Drupal\Core\Link$linkA link object to convert to a string.
A GeneratedLink object containing a link to the given route and parameters and bubbleable metadata.

Implements LinkGeneratorInterface.

References LinkGenerator\generate(), Link\getText(), and Link\getUrl().

Here is the call graph for this function:

The documentation for this class was generated from the following file: