TwigExtension::getLink

public TwigExtension::getLink($text, $url, $attributes = [])

Gets a rendered link from a 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 264

Class

TwigExtension
A class providing Drupal Twig extensions.

Namespace

Drupal\Core\Template

Code

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);
  }
  // The text has been processed by twig already, convert it to a safe object
  // for the render system.
  if ($text instanceof \Twig_Markup) {
    $text = Markup::create($text);
  }
  $build = [
    '#type' => 'link',
    '#title' => $text,
    '#url' => $url,
  ];
  return $build;
}
doc_Drupal
2016-10-29 09:49:40
Comments
Leave a Comment

Please login to continue.