Iterate through multiple value content field in Twig template

I need to take control over the rendering of a field_admin_tags field in a node.html.twig template.

This WORKS:

  • {{ content.field_admin_tags }} — Renders everything (label + all field values)
  • {{ content.field_admin_tags.0 }} — Renders only the first value in the field, and no label

PROBLEM: I have no control over the markup surrounding the tags, e.g. <ul><li>...</li></ul>.

So my idea was to iterate over the render array.

But that DOES NOT work:

{% for tag in content.field_admin_tags %}   {{ tag }} {% endfor %} 

I get: Exception: Object of type Drupal\node\Entity\Node cannot be printed.

I think I'm iterating over the keys/values of the render array vs the items inside the field (if I print out an "X" in each loop, I get 20 X's while I only have a couple of two or three values in this field).

I'd like to iterate over content.field_admin_tags.0, content.field_admin_tags.1, etc.

Any idea how to fix this? Thanks.

Replay

Any idea how to fix this? Yes, use a field twig. There you can put a loop that iterates over the field items and theme this as you like:

field--field-admin.html.twig

<ul>
  {% for item in items %}
    <li>{{ item.content }}</li>
  {% endfor %}
</ul>

Category: theming Time: 2016-07-28 Views: 0
Tags: theming

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.112 (s). 12 q(s)