Problem/Motivation
I tried doing this in a Twig template:
{{ link(item.title, item.url, item.attributes) }}
item.attributes
was a Drupal\Core\Template\Attribute
object and therefore I got this error:
Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("A stray drupal_render() invocation with $is_root_call = TRUE is causing bubbling of attached assets to break.") in "themes/example/templates/menu.html.twig" at line 36. in Twig_Template->displayWithErrorHandling() (line 328 of core/vendor/twig/twig/lib/Twig/Template.php).
Proposed resolution
Let the link() function in Twig accept either an array of attributes or an Attribute object.
Beta phase evaluation
Issue category | Task because it completes a piece of functionality in the Twig layer. |
---|---|
Issue priority | Normal, Attribute objects are common in D8 so this is making them usable in the link context in Twig. |
Unfrozen changes | Unfrozen because it only changes markup by way of link attributes. |
Prioritized changes | Not a prioritized change. |
Disruption | Not disruptive, fully backwards compatible. |
Remaining tasks
None at this time.
User interface changes
n/a
API changes
Small backwards compatible API addition.
Data model changes
n/a
Comment | File | Size | Author |
---|---|---|---|
#31 | make_the_twig_extension-2506151-31.patch | 6.16 KB | joelpittet |
#31 | interdiff.txt | 823 bytes | joelpittet |
#28 | interdiff.txt | 1017 bytes | lauriii |
#28 | make_the_twig_extension-2506151-28.patch | 6.1 KB | lauriii |
#24 | make_the_twig_extension-2506151-24.patch | 5.93 KB | joelpittet |
Comments
Comment #1
star-szrVery interesting point, that came up briefly in #2342745-32: Allow Twig link function to pass in HTML attributes but didn't make it into any patch.
Do you think it should be able to accept either?
Comment #2
Lukas von BlarerI don't have enough knowledge to judge since I don't know the implications this would have. I think yes.
Comment #3
jibranLet's allow both array and object.
Comment #4
joelpittet@Lukas von Blarer try this out and let us know.
Comment #5
jibranLooks perfect to me.
Comment #7
star-szrLooks like a missing use statement for Attribute?
Comment #8
joelpittetMy tests were shit too:)
Comment #9
star-szrLooks RTBC, needs a bit of an issue summary update and beta eval, don't have time right now to do so.
Comment #11
joelpittet@Lukas von Blarer Mind updating the issue summary?
Comment #12
joelpittetComment #13
star-szrComment #14
jibranLet's update some old change notice as well.
Comment #15
star-szr@jibran I agree but I don't think one exists. We could expand the docs on https://www.drupal.org/node/2486991 after commit.
Updated the issue summary. RTBC with a tiny docs addition to add the @return, didn't think that was worth holding this up.
Comment #16
jibranAwesome. RTBC +1. Thanks for reporting the issue @Lukas von Blarer. Nice fix @joelpittet.
Comment #17
joelpittet@jibran Do you happen to know where or if there was an old change record?
Comment #18
tim.plunkettInstead of doing this, it should be on $this->storage:
This should have FQCN: \Drupal\Core\Template\Attribute
instanceof
Also, it seems weird that we have #attributes (and $attributes above) getting a single Attribute
Comment #19
joelpittet@tim.plunkett
re #18.1 It won't merge properly if we don't get the value(). Actually I think this needs another test because it should be a deep merge regardless and that is a bug before this patch.
#18.2 cool thanks, will fix:)
#18.3 interesting didn't know that was the way we were doing it but ok cool.
#18.4 I actually should have put a comment for why I did that because I wanted to explicitly test the attribute object and not assume that special cased conversion. It would do the same thing as we have it but I didn't want the test to make extra assumptions.
Comment #20
tim.plunkett18.1, I just meant skipping the inline @var, and moving it to the property itself.
18.3,
Comment #21
joelpittet@tim.plunkett well you helped me find a bug, so cool anyways:)
Comment #22
joelpittetHere's those fixes from #18
Comment #23
star-szrI'm not sure we should be changing those other instanceOf, just doing the new one lowercase. Otherwise, looks good.
Comment #24
joelpittet@Cottser of course you are right ;) Moved those to a novice follow-up #2515018: Lowercase the instances of camelcase'd instanceOf in core for consistency
Comment #25
star-szrNice, back to RTBC. Thanks @joelpittet @tim.plunkett.
Comment #26
lauriiiThis is not a good thing because at least my texteditor will give an warning for having new syntax inside old syntax :/
Comment #27
joelpittetWe aren't blocking on array syntax diff.
Comment #28
lauriiiLets fix it anyway :)
Comment #29
joelpittetOK, still RTBC;)
Comment #30
alexpottThese docs need updating.
Why is this change necessary?
Comment #31
joelpittet@alexpott re #30.2 Because we need to define any variable your template expects. Added 'attributes' to explicitly pass an Attribute object to the template. I tried to change the logic to make it looser on defining the variables here but got shot down:P If you want clarity, @fabianx is the one to ping.
Thanks for the note on the docs #30.1, missed that.
Comment #32
lauriii#31 addresses #30
Comment #33
alexpottThanks for addressing my feedback. Committed df44c00 and pushed to 8.0.x. Thanks!
Thanks for adding the beta evaluation to the issue summary.