Sep 21, 2020 in Drupal 8, Snippets

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 %}