3

I've got an issue with a field called Track Listing. As the title states it is a Text(plain) type with unlimited possible values so that I can input as many as I'd like. The machine name is: track_listing

As it stands I'm outputting the label and items using: {{ content.field_track_listing }} which gives me this:

Track Listing

But I'd like to be able to wrap the field in an ol tag in addition to every track with a li tag to be able to get a nice ordered numbered list. Using twig templates I've wrapped it accordingly but find it only targets the first value which is in this case the label as seen here:

track listing ordered list

I feel that I should be using a for/if loop in order to get the right values since removing the label only wraps the first item as well. If anyone has an article I can check out to point myself in the right direction I would appreciate it, thank you.

3

You can easily theme a multiple value field in the field template.

To do this put a field twig in mytheme/templates:

field--field-track-listing.html.twig

<div{{ attributes }}>

  <div{{ title_attributes }}>{{ label }}</div>

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

</div>

Here you can change the html markup around each field item {{ item.content }} the same way as you do this in node around the complete field {{ content.field_name }}. This is the concept of nested render elements.

2

Inside node--[content-type-name].html.twig

<ol>

{% for track in content.field_track_listing['#items'].getValue() %}

  <li>{{ track.value }}</li>

{% endfor %}

</ol>

If you need to display the field label, put {{ content.field_track_listing['#title'] }} before the <ol> tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.