2

I'm trying to loop through some entries and if they fulfil a certain condition push them into another array of entries.

{% set todaysEvents = []
{# GET TODAY'S ONE-OFF EVENTS #}
{% set todaysOneOffEvents = craft.entries.section('events').eventDate('= '~today | date("Y-m-d")).find() %}

{# ADD TODAY'S ONE-OFF EVENTS TO TODAY'S EVENTS #}
{% set todaysEvents = todaysEvents|merge(todaysOneOffEvents) %}
{# WORKING BEAUTIFULLY #}       

{# FOR MULTI-DAY EVENTS THE DATES ARE IN A MATRIX FIELD, SO WE CAN'T FILTER USING CRAFT.ENTRIES #}
{# INSTEAD, WE HAVE TO FILTER EVERY ENTRY IN EVENTS WHICH USE THE MULTI-DAY ENTRY TYPE #}
{% for event in craft.entries.section('events').type('eventWithMultipleDays').find() %}
    {# NOW FIND ONLY ENTRIES WITH A MATRIX BLOCK CONTAINING TODAY'S DATE #}
    {% for date in event.dates if date.multiDate == today | date("Y-m-d") %}
        {# CORRECTLY IDENTIFIES ENTRY AS HAVING TODAY'S DATE IN A MATRIX BLOCK #}
        {# SO NOW WE ADD IT TO TODAYSEVENTS ARRAY #}
        {% set todaysEvents = todaysEvents|merge(event) %}
        {# ERROR: THE MERGE FILTER ONLY WORKS WITH ARRAYS OR HASHES #}      
    {% endfor %}
{% endfor %}

What go on? See the error in the last comment. How do I pass only these filtered entries into todaysEvents array?

  • I'm trying to do a similar thing right now, looking forward to answers to this! – hamishtaplin Oct 22 '14 at 15:51
5

The merge filter only works with 2 arrays (or 2 hashes), not with an array and an object. I think that the event you are trying to add is not an array, it is a single event (Entry Model). You might try adding the event using [ event ].

{% set todaysEvents = todaysEvents|merge([event]) %}
  • Sorted. Use square brackets to create the array instead. – Clive Portman Oct 22 '14 at 16:29
  • I updated the answer. – Douglas McDonald Oct 22 '14 at 16:31
  • Thanks Douglas. We got there about the same time. I've removed my comment about curly brackets as they're no longer in the answer. – Clive Portman Oct 22 '14 at 16:34
  • Glad you got it sorted! Those things can be incredibly frustrating. Been there ;) – Douglas McDonald Oct 22 '14 at 16:37
1

Solved by putting the event object into an array using square brackets.

{% set todaysEvents = todaysEvents|merge([event]) %}

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.