Handling Events with multiple dates

I try to build an events-page and the events should have the possibility to enter multiple dates.
For the event-child-page i use the new entries-field with a date field and tried to get it to work with this two topics:

… but I can’t get it to work. :frowning: Can you help me?

here is the code of the events-page:

$recurringEvents = $page
			->children()
			->listed()
			->filterBy('intendedTemplate', 'event-recurring');

		$consolidatedEvents = new Pages();
			foreach($recurringEvents as $recurringEvent) {
				$dates = $recurringEvent->dates()->toEntries();
				foreach($dates as $date) {
					$eventDate = $date->toDate();
					if($eventDate > strtotime('-1 day')) {
						$recurringEvent->setEventDate($eventDate);
						$consolidatedEvents->add($recurringEvent);
					}
				}
			}

and here is the page-model:

<?php
  class EventPage extends Page {

    protected $eventdate;

    public function setEventDate($date) {
      $this->eventdate = $date;
    }
    
  }
?>

There is no sorting at the moment, I wanted to see if I can get a collection with the same event multiple times at first, but I get only one.

$consolidatedEvents->add($recurringEvent) stores the page under its ID as the collection key, so adding the same event again just overwrites the previous entry. Use append() with a unique key instead, e.g.

$consolidatedEvents->append($recurringEvent->id() . '-' . $eventDate, $recurringEvent);

Thanks a lot!

now I get a collection with the same page multiple times. :slight_smile:

But there is still one Problem: The pageModel function seems not to work, the single date is not set for each event.

is the pageModel-Code correct for setting a “eventDate” property?

<?php
  class EventsPage extends Page {

    protected $eventDate;

    public function setEventDate($date) {
      $this->eventDate = $date;
    }
    
  }
?>

Hmm, I think you keep adding the same page object in the loop, so setEventDate overwrites it every time and all of them end up with the last date. Try cloning the page for each date:

$occurrence = $recurringEvent->clone();
$occurrence->setEventDate($eventDate);
$consolidatedEvents->append($recurringEvent->id() . '-' . $eventDate, $occurrence);

Also, if the template is event-recurring, shouldn’t the class be EventRecurringPage instead of EventsPage? Or am I missing something. And you can add a getter to read the date:

class EventRecurringPage extends Page
{
    protected $eventDate;

    public function eventDate()
    {
        return $this->eventDate;
    }

    public function setEventDate($date)
    {
        $this->eventDate = $date;
    }
}

Then $event->eventDate() works in your snippet, and sortBy('eventDate') too.

Big thanks!