2

I have some matrix fields containing a date. I then loop through the entries in a category looking for entries containing a matrix field with a date during the next month. Everytime a matrix field is found, the entry id is added to an array of entries. Like so:

{% for event in craft.entries.section('events').type('eventWithMultipleDays').relatedTo(category).find() %}
    {% for date in event.dates if date.multiDate >= now | date("Y-m-d") and date.multiDate <= now | date_modify('+1 month') %}
        {# ADD THE FILTERED EVENT TO THE CATEGORYSEVENTS ARRAY #}
        {# NOTE SQUARE BRACKETS CREATE AN ARRAY - NECESSARY FOR THE MERGE #}
        {% set categoryEvents = categoryEvents | merge([event]) %}          
    {% endfor %}
{% endfor %}

Not surprisingly, some entries contain several matrix blocks with dates falling within the next month, which means the array is containing duplicate entries. That means when I output the entries array, I'm getting repetition, using this, as you'd expect:

{% for event in categoryEvents %}
    {# details output #}
{% endfor %}

Is there a way to only get unique values out of this array? Or, if not, a better way to create the array perhaps?

Thanks, Clive

3

Holy moly! I've solved it already, Batman. Check the array before adding the event to it, like so:

{% if event not in categoryEvents %}
        {% set categoryEvents = categoryEvents | merge([event]) %}
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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