public function TwigExtension::getLink in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension::getLink()
Gets a rendered link from an url object.
Parameters
string $text: The link text for the anchor tag as a translated string.
\Drupal\Core\Url|string $url: The URL object or string used for the link.
array|\Drupal\Core\Template\Attribute $attributes: An optional array or Attribute object of link attributes.
Return value
array A render array representing a link to the given URL.
File
- core/
lib/ Drupal/ Core/ Template/ TwigExtension.php, line 267 - Contains \Drupal\Core\Template\TwigExtension.
Class
- TwigExtension
- A class providing Drupal Twig extensions.
Namespace
Drupal\Core\TemplateCode
public function getLink($text, $url, $attributes = []) {
if (!$url instanceof Url) {
$url = Url::fromUri($url);
}
if ($attributes) {
if ($attributes instanceof Attribute) {
$attributes = $attributes
->toArray();
}
if ($existing_attributes = $url
->getOption('attributes')) {
$attributes = array_merge($existing_attributes, $attributes);
}
$url
->setOption('attributes', $attributes);
}
$build = [
'#type' => 'link',
'#title' => $text,
'#url' => $url,
];
return $build;
}