Filter and sort by dates from structure field

Is there a simple solution for this? I would like to create a section in blueprint that shows all events that have dates in the future and another section that has dates in the past. An event can have several dates. I would then like to sort by the nearest date. The dates are then managed via a structured field.

Yes, you can achieve this with a combination of a page model and a section query.

Basically you need to do the following:

  1. Create a page model for your event template:
    1. Define a hasUpcomingDate() method that loops over the structure field with the dates and returns either true or false depending on whether there are dates in the future.
    2. Define a nearestDate() method that returns the timestamp of the next upcoming date.
  2. Create a page model for your events template (parent of all events):
    1. Define a upcomingEvents() method that filters the events by 'hasUpcomingDate', '==', true and sorts them by nearestDate.
    2. Define a pastEvents() method that filters the events by 'hasUpcomingDate', '==', false.
  3. You can then use those methods in your page sections with queries like query: page.upcomingEvents and page.pastEvents.

That’s a great explanation. I was able to solve it with the suggestions.

In nearestDate I return YYYY-MM-DD. How can I get it to a different date format in blueprint if I would like to output it? toDate() does not work on this.

I’d recommend to return a timestamp instead, then you can format it in any way you like. Or you add a method argument for the format. Then you can do $page->nearestDate('d.m.Y') for example.

Works perfectly :+1: