Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Using array access on separate arrays leaves the templates messy.
Before:
{% for delta, item in items %}
<li{{ item_attributes[delta] }}>{{ item }}</li>
{% endfor %}
After:
{% for item in items %}
<li{{ item.attributes }}>{{ item.content }}</li>
{% endfor %}
Comment | File | Size | Author |
---|---|---|---|
#56 | interdiff-2229435-52-55.txt | 1.17 KB | lauriii |
#56 | 2229435-field-item-attributes-55.patch | 9.48 KB | lauriii |
#36 | interdiff-2229435-28-36.txt | 2.85 KB | Rade |
#36 | 2229435-field-item-attributes-36.patch | 7.88 KB | Rade |
#28 | 2229435-field-item-attributes-28.patch | 7.87 KB | lauriii |
Comments
Comment #1
joelpittetOk well the variable I want to use seems to fail. It's something to do how caching is acting on the $variables['items'] array.
So I'm going to post one that should pass as well to prove it.
Comment #4
joelpittet1: 2229435-field-item-attributes-1-rename.patch queued for re-testing.
Comment #6
joelpittetThings seem to be better on some of the tests with this change and cross my fingers for all of them. Seems there is a theme function for theme_field__node__title... so I converted that to twig. It was trying to render the items out as a render array.
I added the comments back in so those docs doesn't get lost.
Comment #8
joelpittetRe-roll since the theme_field__node__title got removed/fixed.
Comment #10
joelpittetI wish I can form words to say what is wrong here... closest to something that kinda make sense is
drupal_render
taking the array in and thinking it's a render array in field--node--title.html.twig. This is because I believe field is printing {{ items }} and not looping through the items as it does in the base field template...I'll roll that into this patch and we'll see if those words hold water... arg render API you kill my inner child!
Comment #12
joelpittet10: 2229435-field-item-attributes-10.patch queued for re-testing.
Comment #13
joelpittetWell that kinda proves my point but I put the wrong interdiff, so here's the right one to show the change.
Comment #14
Manuel Garcia CreditAttribution: Manuel Garcia commentedI think what's being proposed makes sense. The patch applies cleanly also.
I do see that in the case of the node title field ends up weird. I went and tried to understand what the heck is going on, but failed misserably. This is over my head so I cant help out more other than testing =)
Comment #15
joelpittetthanks for the review @Manuel Garcia.
The render 'api' is trying to be clever and if you print a nested array like all the fields in title it just goes through them all... It's hard to tell if that is intended but this change puts things one level deeper and 'attributes' key throws errors as not being a valid render array key. I think drupal_render_children() is involved but I didn't dig too deep into it either. Nevertheless
{{ items }}
throws errors with this change and it's unintuitive why/how it even works in the first place...Comment #16
joelpittetI should unassign so others can have a look and review.
Comment #17
joelpittetComment #18
lauriiiRerolled the patch #10
Comment #20
lauriiiFixed the failing tests
Comment #21
rteijeiro CreditAttribution: rteijeiro commentedIf testbot says "green" then it's a RTBC.
I tried to add a few extra attributes and it seems to be working for some fields and taxonomy term fields, also for fields in comments.
Comment #22
alexpottWhat happens if someone adds a class - this looks fragile should it be something like
<div class="field-item{% if item.attributes.class %} {{ item.attributes.class }}{% endif %}"{{ item.attributes|without('class') }}>
Shouldn't this be
{% for item in items %}
?Comment #23
lauriiiI fixed point 1. by using
addClass()
. Shouldn't we have delta there for the class name?Comment #24
joelpittetWay better! And yes you need the delta for that class name, gross but unless we can remove that class it needs to stay. Maybe that can be made into a small follow-up to remove that class and test. Or maybe we can remove it here if we can prove it's not being used in core? Two birds...
You can add that right into the addClass(). No need to declare an array to pass it in. That's only necessary when you are adding a bunch to keep things readable. Here is only one class.
item.attributes.addClass('taxonomy-term-reference-' ~ delta)
Comment #25
lauriiiRemoved the
'taxonomy-term-reference-' ~ delta
class because its not used in core anywhere.Comment #26
joelpittetEven cleaner! Back to RTBC, thanks @lauriii
Comment #27
alexpottNow we don't need the delta :)
Comment #28
lauriiiFix'd :)
Comment #29
tuutti CreditAttribution: tuutti commentedPutting back to rtbc
Comment #30
joelpittethaha, oh yeah:)
Comment #33
joelpittetThose fails look legit but let's see why #25 worked and the other one didn't
Comment #35
joelpittetThese failures are what I ran into before in #8 #10 and #15 :(
Comment #36
Rade CreditAttribution: Rade commentedWhat about trying something like this.
There was some changes in the line numbers since lauriii's patch, those are visible in the interdiff. My changes affect only field.html.twig, field--taxonomy-term-reference.html.twig and theme.inc.
Comment #37
Rade CreditAttribution: Rade commentedSending to testbot.
Comment #39
Rade CreditAttribution: Rade commentedChanged 'attributes' to '#attributes' in comment.module.
Comment #40
lauriiiOnly thing changed between 28 and 39 is that
'attributes'
is now'#attributes'
which we are forced to do. More discussion about same kind of issue here: #2329919: Move views_ui classes from preprocess to templates.Comment #41
yched CreditAttribution: yched commentedI'm wary of {{ item.value }}. This is not "the value", a field item is a map of properties/values. And in some basic field types there is one single property named 'value' - see the potential for confusion here ?
If we have to put several things in the "item" var, then I'd suggest item.content rather than item.value for "the HTML string for the formatted FieldItem".
Comment #42
yched CreditAttribution: yched commentedComment #43
joelpittet@yched does item.data or item.content sit better with you? I do see a potential for confusion now that you mention it.
I'm cool with item.content and will re-roll with that in if you think that's better.
We may also want to do that with the already in core version of item_list as that was the precedence for this.
Comment #44
Rade CreditAttribution: Rade commentedGood point yched. Here is a updated patch where item.value is changed to item.content.
Comment #45
yched CreditAttribution: yched commentedI'm fine with .content
However:
- The different syntax (
{{ item['#attributes'] }}
/{{ item.content }}
), introduced in the recent versions of the patch, somewhat nullifies the interest of the change to begin with ?- The docs in the various field*.html.twig (and the issue summary, btw) need to be updated for that new #attributes syntax. Setting back to NW for that.
- I don't see @mortendk in the discussion here. He had a large effort to refactor the field templates in #2214241: Field default markup - removing the divitis. I'm not sure what's the status of that, I'm pinging him over there.
Comment #46
mortendk CreditAttribution: mortendk commentedWe kinda got intro fixing the bigger issue of classname generated in the preprocess - aka the banana concensus.
Lets do a face to face talk at the sprint in amsterdam ?
Comment #47
lauriiiComment #48
joelpittetSo the whole purpose of this issue was to make this template provide clear variables to the template. '#attributes' is not that.
We need to get this back on track as {{ item.attributes }} and {{ item.content }}
No # keys in templates please.
Comment #49
joelpittetComment #50
lauriiiGoing back to
attribute
without number sign.Comment #52
lauriiiComment #53
Rade CreditAttribution: Rade commentedSeems to be working just fine, setting to RTBC. If anyone else comes up with problems with this, please change the status back again.
Comment #54
lauriiiComment #55
star-szrCan we make this docs modification to field--node--uid.html.twig and field--node--created as well please?
Comment #56
lauriiiComment #57
davidhernandezSetting back to rtbc based on lauriii's interdiff.
Comment #60
lauriiiputting back to rtbc
Comment #61
alexpottLess loops++ nice work.
Committed cbd36ff and pushed to 8.0.x. Thanks!
Comment #64
joelpittetIS update for actual patch