Commit 17422375 authored by alexpott's avatar alexpott

Issue #2744517 by webflo: Twig template link() call to accept markup

parent 2a3217ce
......@@ -8,6 +8,7 @@
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Render\AttachmentsInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\RenderableInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
......@@ -273,6 +274,11 @@ public function getLink($text, $url, $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,
......
......@@ -31,7 +31,7 @@ interface LinkGeneratorInterface {
* This keeps the context of the link title ('settings' in the example) for
* translators.
*
* @param string|array $text
* @param string|array|\Drupal\Component\Render\MarkupInterface $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
......
......@@ -2,6 +2,7 @@
namespace Drupal\system\Tests\Theme;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
......@@ -75,12 +76,14 @@ public function testTwigLinkGenerator() {
/** @var \Drupal\Core\Utility\LinkGenerator $link_generator */
$link_generator = $this->container->get('link_generator');
$expected = [
'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE])),
'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE, 'attributes' => ['foo' => 'bar']])),
'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar', 'id' => 'kitten']])),
'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['id' => 'kitten']])),
'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['class' => ['llama', 'kitten', 'panda']]])),
'link via the linkgenerator: ' . $link_generator->generate(Markup::create('<span>register</span>'), new Url('user.register', [], ['absolute' => TRUE])),
];
// Verify that link() has the ability to bubble cacheability metadata:
......
......@@ -3,3 +3,5 @@
<div>link via the linkgenerator: {{ link('register', test_url_attribute, {'id': 'kitten'}) }}</div>
<div>link via the linkgenerator: {{ link('register', 'route:user.register', {'id': 'kitten'}) }}</div>
<div>link via the linkgenerator: {{ link('register', 'route:user.register', attributes) }}</div>
{% set title %}<span>register</span>{% endset %}
<div>link via the linkgenerator: {{ link(title, test_url) }}</div>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment