Create a comma-separated list from a Multi-Value field in Drupal 8

Example Use Case

Strategy

1. Create the template

2. Looking at the template

  1. Single-value field with the label hidden
  2. Multi-value field with the label hidden
  3. Single-value field with the label shown
  4. Multi-value field with the label shown

3. The code

{% for item in items %}
{% if loop.last %}
{% set separator = '' %}
{% else %}
{% set separator = ', ' %}
{% endif %}

<li{{ item.attributes}}>
{{ item.content }}{{ separator }}
</li>
{% endfor %}

--

--

--

I’m a front-end developer and web designer and love to code a lot.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sarah Carney

Sarah Carney

I’m a front-end developer and web designer and love to code a lot.

More from Medium

Front-end buried point — Error monitoring

Top 10 reasons to use bootstrap framework

Requiring an event before each iteration of a “for” loop (using async functions and promise…

overview image of the final code solution

Printing numbers line by line with a breakpoint.