Problem/Motivation

I tried to render a Url object with attributes twice on inside a template. The attributes are lost after the first rendering.

Proposed resolution

Clone the Url object before rendering, the link function should have no effect on the passed url object.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

webflo created an issue. See original summary.

webflo’s picture

webflo’s picture

Issue tags: +Twig

Status: Needs review » Needs work

The last submitted patch, 2: 2842399-1-test-only.patch, failed testing.

webflo’s picture

Status: Needs work » Needs review
joelpittet’s picture

@webflo, I'm quite curious what removed the attributes, this solution could work but maybe better if we find where the attributes are being removed because that may have other negative effects elsewhere in the system?

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

Issue tags: +Immutability
webflo’s picture

dawehner’s picture

Component: theme system » routing system
FileSize
1.58 KB

This is a unit test which renders the link twice. Its a bug in the underlying link generator.

dawehner’s picture

Status: Needs review » Needs work

The last submitted patch, 10: 2842399-10.patch, failed testing.

webflo’s picture

Title: Unable to render link with attributes twice in a twig template » Unable to render link with attributes twice

The root cause is in LinkGenerator.

webflo’s picture

Status: Needs work » Needs review
FileSize
3.84 KB

This is a combination of #10 and the test-only patch from #2

Status: Needs review » Needs work

The last submitted patch, 14: 2842399-14.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
6.39 KB
3.64 KB

@jorges and @dawehner debugged the remaining failure. Turns out, we need the original fix from @webflo as well.

dawehner’s picture

This time with less commented out.

webflo’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, nice test coverage in the LinkGenerator und TwigExtension.

tim.plunkett’s picture

+++ b/core/lib/Drupal/Core/Template/TwigExtension.php
@@ -280,6 +280,10 @@ public function getLink($text, $url, $attributes = []) {
+    // The twig extension should not modify the original url object, this

+++ b/core/lib/Drupal/Core/Utility/LinkGenerator.php
@@ -76,6 +76,11 @@ public function generateFromLink(Link $link) {
+    // The link generator should not modify the original url object, this

Nit, s/url/URL

Fix looks good.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Url should so be immutable.

Committed and pushed 9c77748 to 8.4.x and 91d653d to 8.3.x. Thanks!

diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php
index ed2b4c0..9f25959 100644
--- a/core/lib/Drupal/Core/Template/TwigExtension.php
+++ b/core/lib/Drupal/Core/Template/TwigExtension.php
@@ -280,7 +280,7 @@ 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
+    // The twig extension should not modify the original URL object, this
     // ensures consistent rendering.
     // @see https://www.drupal.org/node/2842399
     $url = clone $url;
diff --git a/core/lib/Drupal/Core/Utility/LinkGenerator.php b/core/lib/Drupal/Core/Utility/LinkGenerator.php
index ed022a6..1dad742 100644
--- a/core/lib/Drupal/Core/Utility/LinkGenerator.php
+++ b/core/lib/Drupal/Core/Utility/LinkGenerator.php
@@ -76,7 +76,7 @@ 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
+    // The link generator should not modify the original URL object, this
     // ensures consistent rendering.
     // @see https://www.drupal.org/node/2842399
     $url = clone $url;

Fixed on commit.

  • alexpott committed 9c77748 on 8.4.x
    Issue #2842399 by dawehner, webflo: Unable to render link with...

  • alexpott committed 91d653d on 8.3.x
    Issue #2842399 by dawehner, webflo: Unable to render link with...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.