public LinkGeneratorInterface::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.
@internal Should not be used in user code. Use \Drupal\Core\Link instead.
string|array $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\Utility\SafeMarkup::format().
\Drupal\Core\Url $url: The URL object used for the link. Amongst its options, the following may be set to affect the generated link:
\Drupal\Core\GeneratedLink A GeneratedLink object containing a link to the given route and parameters and bubbleable metadata.
\Symfony\Component\Routing\Exception\RouteNotFoundException Thrown when the named route doesn't exist.
\Symfony\Component\Routing\Exception\MissingMandatoryParametersException Thrown when some parameters are missing that are mandatory for the route.
\Symfony\Component\Routing\Exception\InvalidParameterException Thrown when a parameter value for a placeholder is not correct because it does not match the requirement.
public function generate($text, Url $url);
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Utility!LinkGeneratorInterface.php/function/LinkGeneratorInterface::generate/8.1.x