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:
- Create a page model for your event template:
- Define a
hasUpcomingDate()
method that loops over the structure field with the dates and returns eithertrue
orfalse
depending on whether there are dates in the future. - Define a
nearestDate()
method that returns the timestamp of the next upcoming date.
- Define a
- Create a page model for your events template (parent of all events):
- Define a
upcomingEvents()
method that filters the events by'hasUpcomingDate', '==', true
and sorts them bynearestDate
. - Define a
pastEvents()
method that filters the events by'hasUpcomingDate', '==', false
.
- Define a
- You can then use those methods in your page sections with queries like
query: page.upcomingEvents
andpage.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