I have events master pages that have multiple dates that are maintained via structured fields. Subpages for the events seemed too cumbersome to me.
However, it is now the case that each event should actually be its own page. The call should not be a problem with a custom route from slug and date.
However, I am having difficulties with the template overviews. At the moment I’m using a loop through the dates to create duplicates of the page and add the date as an object variable. Is there a better solution?
foreach($events as $event){
$dates = $event->dates()->toStructure();
foreach($dates as $date){
$eventDate = $date->date()->toDate('Ymd');
$eventTime = $date->time();
$eventLocation = $date->location();
$toDate = date( "Ymd" , strtotime("now +4 Weeks"));
if($eventDate <= $toDate && $eventDate >= date("Ymd")){
$eventClone = clone $event;
$eventClone->eventDate = $eventDate;
$eventClone->eventLocation = $eventLocation;
$eventClone->eventTime = $eventTime;
$sortedEvents[] = $eventClone;
}
}
}
usort($sortedEvents, function($a, $b) {
return $a->eventDate <=> $b->eventDate;
});