How to remove duplicate values in this code, if they are in "{{ post.entity.name.value }}"?

{% block paragraph %}{% block content %}

    {% for post in paragraph.field_post %}
        {% if loop.last %}
            {% set separator = '' %}
        {% else %}
            {% set separator = ',' %}
        {% endif %}
        {{ post.entity.name.value }}{{ separator }}
    {% endfor %}
{% endblock %}{% endblock paragraph %}

I have a "Paragraph" type field. In the paragraph I have two fields: place of work (node reference) and position (taxonomy field).

Sometimes there is a need to display only the values of taxonomy fields on the site, and they can be repeated, because there may be several places of work. An employee can work in the same positions in several places. How do I remove duplicates in the paragraph display mode template?

As usual, I turned to ChatGPT, she gave me this solution

{% for post in paragraph.field_post %}{% set unique_names = post.entity.name.value|split(',')|batch(1)|map(item => item|unique)|join(',') %}{{ unique_names }}{% endfor %}

But there is a problem. The "unique" filter is not a standard filter in Twig. It can only be accessed by using third-party Twig extensions or Composer packages :(

1 Answer 1


Since we output ready-made values in the tiwg templates and avoid complex code, the best solution in this situation turned out to be using the view module. I created the view I needed and set up the context, enabled aggregation, and then embedded it in the right place on the page.

Using the paragraph display mode template for displaying posts turned out to be not a flexible solution for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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