LinkGenerator.php in Drupal 8
File
core/lib/Drupal/Core/Utility/LinkGenerator.php
View source
<?php
namespace Drupal\Core\Utility;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\GeneratedLink;
use Drupal\Core\GeneratedButton;
use Drupal\Core\GeneratedNoLink;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
class LinkGenerator implements LinkGeneratorInterface {
protected $urlGenerator;
protected $moduleHandler;
protected $renderer;
public function __construct(UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, RendererInterface $renderer) {
$this->urlGenerator = $url_generator;
$this->moduleHandler = $module_handler;
$this->renderer = $renderer;
}
public function generateFromLink(Link $link) {
return $this
->generate($link
->getText(), $link
->getUrl());
}
public function generate($text, Url $url) {
$url = clone $url;
$url
->setUrlGenerator($this->urlGenerator);
if (is_array($text)) {
$text = $this->renderer
->render($text);
}
$variables = [
'text' => $text,
'url' => $url,
'options' => $url
->getOptions(),
];
$variables['options'] += [
'attributes' => [],
'query' => [],
'language' => NULL,
'set_active_class' => FALSE,
'absolute' => FALSE,
];
if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) {
$variables['options']['attributes']['hreflang'] = $variables['options']['language']
->getId();
}
array_walk($variables['options']['query'], function (&$value) {
if ($value instanceof MarkupInterface) {
$value = (string) $value;
}
});
if (!empty($variables['options']['set_active_class']) && !$url
->isExternal()) {
if (!empty($variables['options']['query'])) {
$query = $variables['options']['query'];
ksort($query);
$variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query);
}
if ($url
->isRouted() && !isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
$system_path = $url
->getInternalPath();
if ($url
->getRouteName() === '<front>') {
$system_path = '<front>';
}
if (!empty($system_path)) {
$variables['options']['attributes']['data-drupal-link-system-path'] = $system_path;
}
}
}
if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
$variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
}
$this->moduleHandler
->alter('link', $variables);
$url = $variables['url'];
$attributes = [
'href' => '',
] + $variables['options']['attributes'];
unset($variables['options']['attributes']);
$url
->setOptions($variables['options']);
if ($url
->isExternal()) {
$generated_link = new GeneratedLink();
$attributes['href'] = $url
->toString(FALSE);
return $this
->doGenerate($generated_link, $attributes, $variables);
}
if ($url
->isRouted() && $url
->getRouteName() === '<nolink>') {
$generated_link = new GeneratedNoLink();
unset($attributes['href']);
return $this
->doGenerate($generated_link, $attributes, $variables);
}
if ($url
->isRouted() && $url
->getRouteName() === '<button>') {
$generated_link = new GeneratedButton();
$attributes['type'] = 'button';
unset($attributes['href']);
return $this
->doGenerate($generated_link, $attributes, $variables);
}
$generated_url = $url
->toString(TRUE);
$generated_link = GeneratedLink::createFromObject($generated_url);
$attributes['href'] = $generated_url
->getGeneratedUrl();
return $this
->doGenerate($generated_link, $attributes, $variables);
}
protected function doGenerate($generated_link, $attributes, $variables) {
if (!$variables['text'] instanceof MarkupInterface) {
$variables['text'] = Html::escape($variables['text']);
}
$attributes = new Attribute($attributes);
return $generated_link
->setGeneratedLink('<' . $generated_link::TAG . $attributes . '>' . $variables['text'] . '</' . $generated_link::TAG . '>');
}
}
Classes
Name |
Description |
LinkGenerator |
Provides a class which generates a link with route names and parameters. |