1

I have a list of forms which I want to sort by day:

{% set groupVerbund = 'datum.day' %}

{% set allSubmissions = craft.amForms.submissions({
   formHandle: 'reservation',
   limit: null}).find()
%}
{% set entriesByDay = allSubmissions | group(groupVerbund) %}


{% for entriesInDay in entriesByDay %}
  <ul>
    {% for entry in entriesInDay %}
      {% if loop.first %}<h2>{{ entry.datum | date('d.m.Y') }}</h2>{% endif %}
       <li>{{ entry.datum | date('d. M Y') }} | {{ entry.vorname }} {{ entry.nachname }}</li>
    {% endfor %}
  </ul>
{% endfor %}

This works but there are two entries which just are at the wrong place:

Entries in: 22. Okt. 2016
------------------
22. Okt. 2016 | Name Surname
22. Okt. 2016 | Name Surname
22. Okt. 2016 | Name Surname
22. Okt. 2016 | Name Surname
22. Okt. 2016 | Name Surname
22. Okt. 2016 | Name Surname
22. Okt. 2016 | Name Surname
22. Okt. 2016 | Name Surname
22. Jan. 2017 | Name Surname // This one is wrong
22. Okt. 2016 | Name Surname
22. Okt. 2016 | Name Surname

Entries in: 13. Nov.2016
------------------
13. Nov. 2016 | Name Surname
13. Dez. 2016 | Name Surname // This one is wrong
13. Nov. 2016 | Name Surname

Everything else is at the right place except these two entries. {{ entry.datum}} is a custom date field so it's not possible to add a wrong date format.

How can this be?

4

When you specifically group on dateField.day (in your case, the |group('datum.day') part), the group filter won't care about the month or year for the date field – only the day. This means that any entry posted on the 22nd of any month in any year will be grouped together, which is probably not what you want.

In this case, the solution is simply to group on the date field without a postfix (which will create an array index for each unique day/date):

{% set groupVerbund = 'datum' %}
{% set entriesByDay = allSubmissions | group(groupVerbund) %}

Additionally, I can't help but notice the <h2> tag inside your <ul>, which is strictly speaking non-valid HTML. A better solution might be to do something like this (this would also save you a conditional):

{% for day, entries in entriesByDay %}
    <div class="day">
        <h2>{{ day|date('d.m.Y') }}</h2>
        <ul>
            {% for entry in entries %}
                {{ entry.datum | date('d. M Y') }} | {{ entry.vorname }} {{ entry.nachname }}
            {% endfor %}
        </ul>
    </div>
{% endfor %}

One final issue is sorting – you'll probably see the dates mixed up, which is just how the |group filter works – it only groups, and cares less about sorting. Basically, the group filter will return an array that looks something like this:

[
    '2016-10-22' => [array of entries],
    '2017-01-22' => [array of entries],
    '2016-11-13' => [array of entries],
]

...which means that if you want the output to be sorted by date, you'll need to sort that array on its keys (i.e. the dates) – something Twig's native sort filter regrettably doesn't do, but which is easily accomplished with the fantastic SuperSort plugin:

{% set entriesByDay = allSubmissions | group(groupVerbund) | supersort('ksort') %}

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.