2

I'm struggling with filtering future dates in a Matrix block that contains a date field.

I would like to display 'Open' or 'Closed' as the result of a condition checking if the value of matrix rows is today, but I only want to run the condition against future dates in the matrix block. Past dates should be ignored.

In all examples I have seen, the search logic is at declared at the entries-level, but this is a matrix block and I can't get the two to connect.

My code...

{% set closed = craft.block('closedDates')
    .type('closedDate')
    .date('>=' ~ today)
    .all() %}
{% for block in entry.closedDates.all() %}
    {% if today == closed %}
        <p><strong>Closed</strong></p>
    {% else %}
        <p>Open</p>
    {% endif %}
{% endfor %}```

The (matrix) field = closedDates, the block type = closedDate, the field = date.

I prefer a matrix field so I have only 1 entry.

Would appreciate any suggestions.
  • 1
    Your matrix field consists of a date and you set the date as a "closed date" right? So block.date is the date where it is closed? – Marten Brosch May 28 at 7:21
2

Thanks Marten, yes, that's correct.

After plugging away I have gotten it to work, but it's likely not as efficient as it could be...

{% set today = now|date('Y-m-d') %}
{% set todayday = now|date('l') %}
{% for block in entry.closedDates.date('>=' ~ today).limit(1) %}
    {% set closedDate = block.date.format('Y-m-d') %}
    {% if closedDate == today %}
        <p><strong>It's a holiday, sorry, we're closed</strong></p>
    {% elseif todayday == 'Sunday' %}
        <p><strong>It's {{ todayday }}, sorry, we're closed</strong></p>
    {% else %}
        <p>We're Open!</p>
    {% endif %}
{% endfor %}

I added a check for whether today is Sunday, which is a normally closed day that would not be in the entry.

Doug

| improve this answer | |

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.