Score:-2

How do I remove duplicate taxonomy field values in the paragraph display mode template?

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 :(

Score:0

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.