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 %}
CommentFileSizeAuthor
#56 interdiff-2229435-52-55.txt1.17 KBlauriii
#56 2229435-field-item-attributes-55.patch9.48 KBlauriii
#52 2229435-field-item-attributes-52.patch8.83 KBlauriii
#52 interdiff-2229435-50-52.txt698 byteslauriii
#50 interdiff-2229435-47-50.txt3.77 KBlauriii
#50 2229435-field-item-attributes-50.patch7.89 KBlauriii
#47 interdiff-2229435-44-47.txt1.99 KBlauriii
#47 2229435-field-item-attributes-47.patch7.9 KBlauriii
#44 interdiff-2229435-39-44.txt8.3 KBRade
#44 2229435-field-item-attributes-44.patch7.89 KBRade
#39 2229435-field-item-attributes-39.patch7.88 KBRade
#39 interdiff-2229435-36-39.txt614 bytesRade
#36 interdiff-2229435-28-36.txt2.85 KBRade
#36 2229435-field-item-attributes-36.patch7.88 KBRade
#28 2229435-field-item-attributes-28.patch7.87 KBlauriii
#28 interdiff-2229435-25-28.txt598 byteslauriii
#25 2229435-field-item-attributes-25.patch7.84 KBlauriii
#25 interdiff-2229435-23-25.txt713 byteslauriii
#23 interdiff-2229435-20-23.txt2 KBlauriii
#23 2229435-field-item-attributes-23.patch7.97 KBlauriii
#21 fields-attributes.png38.96 KBrteijeiro
#20 2229435-field-item-attributes-20.patch7.43 KBlauriii
#20 interdiff-2229435-18-20.txt691 byteslauriii
#18 2229435-field-item-attributes-18.patch6.76 KBlauriii
#13 interdiff.txt808 bytesjoelpittet
#10 2229435-field-item-attributes-10.patch6.62 KBjoelpittet
#10 interdiff.txt2.2 KBjoelpittet
#8 2229435-field-item-attributes-8.patch5.83 KBjoelpittet
#6 interdiff.txt4.76 KBjoelpittet
#6 2229435-field-item-attributes-6.patch8.39 KBjoelpittet
#1 2229435-field-item-attributes-1-rename.patch4.68 KBjoelpittet
#1 interdiff.txt2.48 KBjoelpittet
#1 2229435-field-item-attributes-1.patch4.55 KBjoelpittet
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joelpittet’s picture

Status: Active » Needs review
FileSize
4.55 KB
2.48 KB
4.68 KB

Ok 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.

The last submitted patch, 1: 2229435-field-item-attributes-1.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 1: 2229435-field-item-attributes-1-rename.patch, failed testing.

joelpittet’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 1: 2229435-field-item-attributes-1-rename.patch, failed testing.

joelpittet’s picture

Assigned: Unassigned » joelpittet
Status: Needs work » Needs review
Issue tags: +Twig, +theme system cleanup, +sprint
FileSize
8.39 KB
4.76 KB

Things 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.

Status: Needs review » Needs work

The last submitted patch, 6: 2229435-field-item-attributes-6.patch, failed testing.

joelpittet’s picture

Status: Needs work » Needs review
FileSize
5.83 KB

Re-roll since the theme_field__node__title got removed/fixed.

Status: Needs review » Needs work

The last submitted patch, 8: 2229435-field-item-attributes-8.patch, failed testing.

joelpittet’s picture

Status: Needs work » Needs review
FileSize
2.2 KB
6.62 KB

I 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!

Status: Needs review » Needs work

The last submitted patch, 10: 2229435-field-item-attributes-10.patch, failed testing.

joelpittet’s picture

Status: Needs work » Needs review
joelpittet’s picture

FileSize
808 bytes

Well that kinda proves my point but I put the wrong interdiff, so here's the right one to show the change.

Manuel Garcia’s picture

I 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 =)

joelpittet’s picture

thanks 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...

joelpittet’s picture

Assigned: joelpittet » Unassigned

I should unassign so others can have a look and review.

joelpittet’s picture

Issue summary: View changes
lauriii’s picture

Issue tags: +FUDK
FileSize
6.76 KB

Rerolled the patch #10

Status: Needs review » Needs work

The last submitted patch, 18: 2229435-field-item-attributes-18.patch, failed testing.

lauriii’s picture

Status: Needs work » Needs review
FileSize
691 bytes
7.43 KB

Fixed the failing tests

rteijeiro’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
38.96 KB

If 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.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/system/templates/field.html.twig
    @@ -34,8 +35,8 @@
    +      <div class="field-item"{{ item.attributes }}>{{ item.value }}</div>
    
    +++ b/core/themes/bartik/templates/field--taxonomy-term-reference.html.twig
    @@ -22,7 +23,7 @@
    +      <li class="taxonomy-term-reference-{{ delta }}"{{ item.attributes }}>{{ item.value }}</li>
    

    What 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') }}>

  2. +++ b/core/themes/bartik/templates/field--taxonomy-term-reference.html.twig
    @@ -22,7 +23,7 @@
         {% for delta, item in items %}
    

    Shouldn't this be {% for item in items %}?

lauriii’s picture

Status: Needs work » Needs review
FileSize
7.97 KB
2 KB

I fixed point 1. by using addClass(). Shouldn't we have delta there for the class name?

joelpittet’s picture

Status: Needs review » Needs work

Way 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...

+++ b/core/themes/bartik/templates/field--taxonomy-term-reference.html.twig
@@ -9,20 +9,26 @@
+      {%
+        set item_classes = [
...
+        ]
+      %}

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)

lauriii’s picture

Status: Needs work » Needs review
FileSize
713 bytes
7.84 KB

Removed the 'taxonomy-term-reference-' ~ delta class because its not used in core anywhere.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

Even cleaner! Back to RTBC, thanks @lauriii

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/themes/bartik/templates/field--taxonomy-term-reference.html.twig
@@ -9,20 +9,21 @@
     {% for delta, item in items %}
-      <li class="taxonomy-term-reference-{{ delta }}"{{ item_attributes[delta] }}>{{ item }}</li>
+      <li{{ item.attributes }}>{{ item.value }}</li>
     {% endfor %}

Now we don't need the delta :)

lauriii’s picture

Status: Needs work » Needs review
FileSize
598 bytes
7.87 KB

Fix'd :)

tuutti’s picture

Status: Needs review » Reviewed & tested by the community

Putting back to rtbc

joelpittet’s picture

haha, oh yeah:)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 28: 2229435-field-item-attributes-28.patch, failed testing.

Status: Needs work » Needs review
joelpittet’s picture

Those fails look legit but let's see why #25 worked and the other one didn't

Status: Needs review » Needs work

The last submitted patch, 28: 2229435-field-item-attributes-28.patch, failed testing.

joelpittet’s picture

These failures are what I ran into before in #8 #10 and #15 :(

Rade’s picture

What 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.

Rade’s picture

Status: Needs work » Needs review

Sending to testbot.

Status: Needs review » Needs work

The last submitted patch, 36: 2229435-field-item-attributes-36.patch, failed testing.

Rade’s picture

Status: Needs work » Needs review
FileSize
614 bytes
7.88 KB

Changed 'attributes' to '#attributes' in comment.module.

lauriii’s picture

Status: Needs review » Reviewed & tested by the community

Only 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.

yched’s picture

I'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".

yched’s picture

Issue tags: +Entity Field API
joelpittet’s picture

Assigned: Unassigned » yched
Status: Reviewed & tested by the community » Needs work
Issue tags: +Novice

@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.

Rade’s picture

Status: Needs work » Needs review
FileSize
7.89 KB
8.3 KB

Good point yched. Here is a updated patch where item.value is changed to item.content.

yched’s picture

Status: Needs review » Needs work

I'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.

mortendk’s picture

We 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 ?

lauriii’s picture

Status: Needs work » Needs review
FileSize
7.9 KB
1.99 KB
joelpittet’s picture

+++ b/core/modules/system/templates/field.html.twig
@@ -31,11 +32,11 @@
+    {% for item in items %}
+      <div{{ item['#attributes'].addClass('field-item') }}>{{ item.content }}</div>

So 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.

joelpittet’s picture

Status: Needs review » Needs work
lauriii’s picture

Status: Needs work » Needs review
FileSize
7.89 KB
3.77 KB

Going back to attribute without number sign.

Status: Needs review » Needs work

The last submitted patch, 50: 2229435-field-item-attributes-50.patch, failed testing.

lauriii’s picture

Status: Needs work » Needs review
FileSize
698 bytes
8.83 KB
Rade’s picture

Status: Needs review » Reviewed & tested by the community

Seems to be working just fine, setting to RTBC. If anyone else comes up with problems with this, please change the status back again.

lauriii’s picture

Assigned: yched » Unassigned
star-szr’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/node/templates/field--node--title.html.twig
@@ -8,11 +8,17 @@
- * - items: List of all the field items.
+ * - items: List of all the field items. Each item contains:
+ *   - attributes: List of HTML attributes for each item.
+ *   - content: The field item content.

Can we make this docs modification to field--node--uid.html.twig and field--node--created as well please?

lauriii’s picture

Status: Needs work » Needs review
FileSize
9.48 KB
1.17 KB
davidhernandez’s picture

Status: Needs review » Reviewed & tested by the community

Setting back to rtbc based on lauriii's interdiff.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 56: 2229435-field-item-attributes-55.patch, failed testing.

lauriii’s picture

Status: Needs work » Reviewed & tested by the community

putting back to rtbc

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Less loops++ nice work.

Committed cbd36ff and pushed to 8.0.x. Thanks!

  • alexpott committed cbd36ff on 8.0.x
    Issue #2229435 by lauriii, joelpittet, Rade: Clean up the way attributes...

Status: Fixed » Closed (fixed)

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

joelpittet’s picture

Issue summary: View changes

IS update for actual patch