0

I have a section called "events" in which I have a table field that contains dates and times of these events.

So I want to order my events by date and within each day by time.

I thought of mergin an array that contains only the basic stuff and the Id to output the rest of the entry. (yes! I will cache the template).

This is how far I got, but I am struggling on how to first order by date, and then within a day, by time.

{% set entries = craft.entries.section('events').limit(null) %}

{% set dates = [] %}

{% for entry in entries %}
    {% set table = entry.cf_dates_table %}

    {% for row in table if row.startDate %}
        {% set dates = dates|merge(
            [{
                'startDate': row.startDate,
                'startTime': row.startTime|date('G:i'),
                'endTime': row.endTime|date('G:i'),
                'id': entry.id,
                'uniqueId': entry.id ~ '-' ~ loop.index,
            }]
        )%}
    {% endfor %}
{% endfor %}


<div id="list_container">
    {% for date in dates|sort() %}
        {{ date.startDate|date('l, d. F') }} {{ date.startTime }} – {{ date.endTime }}, {{ date.id }}<br />
    {% endfor %}
</div>

I could imagine grouping the array by the day and then sort them within each day. But I can't get it to work.

Could somebody point me into the right direction?
Is this a viable approach?

Cheers
Stefan

0

Thanks to brad I got the whole thing working. Here's the stripped down code:

{#

    Get all Dates from within the table field in the section veranstaltungen
    ------------------------------------------------------------------------
    We have a section called veranstaltungen with a table field that contains dates and times
    In order to get all occurences of the dates wihin the table field, we first get all of the
    table dates and attach the entry to each table date.
    Then we sort this array (dates) first by day and within a day by time.

    This is a simple Calendar without the need for a plugin.

#}
{% set entries = craft.entries({
    section: 'veranstaltungen',
    with: [
        'cf_veranstaltungs_cat',
        'cf_veranstaltungsDatum_table',
        'cf_veranstaltung_asset',
        'cf_veranstaltungsLinks_table',
        'cf_veranstaltungs_assets',
        'cf_coVeranstalter_user'
    ],

    relatedTo : relation_param,
    authorId: veranstalter_param|trim,

    limit: null
}) %}

{#
    The dates Array will contain all occurences of the entries - we will order it first by date and then by time
#}
{% set dates = [] %}

{% for entry in entries %}

    {% set table = entry.cf_veranstaltungsDatum_table %}

    {% for row in table if row.startDate %}
        {% set dates = dates|merge(
            [{
                'date': row.startDate|date('Y-m-d'),
                'time': row.startTime|date('G'),
                'startDate': row.startDate,
                'startTime': row.startTime|date('G:i'),
                'endTime': row.endTime|date('G:i'),
                'entry': entry,
                'links': entry.cf_veranstaltungsLinks_table,
                'image': entry.cf_veranstaltung_asset,
                'files': entry.cf_veranstaltungs_assets,
                'categories': entry.cf_veranstaltungs_cat ?? null,
                'categoryColor': entry.cf_veranstaltungs_cat[0].cf_veranstaltungsCatColor ?? null,
                'categoryIcon': entry.cf_veranstaltungs_cat[0].cf_veranstaltungsIcon_asset.first.url ?? null,
                'author': entry.author,
                'coVeranstalter': entry.cf_coVeranstalter_user,
                'id': entry.id,
            }]
        ) %}
    {% endfor %}
{% endfor %}


{#
    ------------------------
    Veranstaltungen Listing
    ------------------------
#}

<div id="list_container">

    {% set datesSortedByDay = dates|group('date')|supersort('ksort', 'date') %}
    {% for key, day in datesSortedByDay %}

        <h2>{{ key|date('l, d. F') }}</h2>

        {% for date in day|supersort('sort', 'time') %}
            {% include "veranstaltungen/_common_tableDates" %}
        {% endfor %}
    {% endfor %}
</div>

Crazy what you can do with twig!

  • I guess I still have a N+1 Problem, because the page loads with 240 queries, and the dates array has a size of 153. I've tried to eager load everything possible, but must have forgotten something. maybe somebody can spot the error? – outline4 May 22 '18 at 19:55

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.