Commit 9c777482 authored by alexpott's avatar alexpott
Browse files

Issue #2842399 by dawehner, webflo: Unable to render link with attributes twice

parent f5be97cb
......@@ -280,6 +280,10 @@ public function getLink($text, $url, $attributes = []) {
if (!$url instanceof Url) {
$url = Url::fromUri($url);
}
// The twig extension should not modify the original URL object, this
// ensures consistent rendering.
// @see https://www.drupal.org/node/2842399
$url = clone $url;
if ($attributes) {
if ($attributes instanceof Attribute) {
$attributes = $attributes->toArray();
......
......@@ -76,6 +76,11 @@ public function generateFromLink(Link $link) {
* @see system_page_attachments()
*/
public function generate($text, Url $url) {
// The link generator should not modify the original URL object, this
// ensures consistent rendering.
// @see https://www.drupal.org/node/2842399
$url = clone $url;
// Performance: avoid Url::toString() needing to retrieve the URL generator
// service from the container.
$url->setUrlGenerator($this->urlGenerator);
......
......@@ -86,6 +86,7 @@ public function testTwigLinkGenerator() {
'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])),
'link via the linkgenerator: <a href="' . $generated_url . '"><span>register</span><svg></svg></a>',
'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar']])) . ' ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar']])),
];
// Verify that link() has the ability to bubble cacheability metadata:
......
......@@ -7,3 +7,4 @@
<div>link via the linkgenerator: {{ link(title, test_url) }}</div>
{% set title %}<span>register</span><svg></svg>{% endset %}
<div>link via the linkgenerator: {{ link(title, test_url) }}</div>
<div>link via the linkgenerator: {{ link('register', test_url_attribute) }} {{ link('register', test_url_attribute) }}</div>
......@@ -537,6 +537,25 @@ public function testGenerateWithAlterHook() {
$this->assertEquals($expected_link_markup, (string) $this->linkGenerator->generate('Test', $url)->getGeneratedLink());
}
/**
* Tests whether rendering the same link twice works.
*
* This is a regression test for https://www.drupal.org/node/2842399.
*/
public function testGenerateTwice() {
$this->urlGenerator->expects($this->any())
->method('generateFromRoute')
->will($this->returnValue((new GeneratedUrl())->setGeneratedUrl('/')));
$url = Url::fromRoute('<front>', [], ['attributes' => ['class' => ['foo', 'bar']]]);
$url->setUrlGenerator($this->urlGenerator);
$link = Link::fromTextAndUrl('text', $url);
$link->setLinkGenerator($this->linkGenerator);
$output = $link->toString() . $link->toString();
$this->assertEquals('<a href="/" class="foo bar">text</a><a href="/" class="foo bar">text</a>', $output);
}
/**
* Checks that a link with certain properties exists in a given HTML snippet.
*
......
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