Loop through entity reference in Twig to get id's in drupal 8

Entity references are widely used in drupal to link to other entity objects. In this case I had to loop over them in Twig to get a string with their id's, divided by a plus sign. Here is the snippet:

{% set ids = '' %}
{% for key, value in paragraph.field_popup_reference.value %}
  {% if key > 0 %}
    {% set ids = ids ~ '+' ~ value.target_id %}
  {% else %}
    {% set ids = value.target_id %}
  {% endif %}
{% endfor %}

 

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!