Hi friends ![]()
is it true, that i cant add the same page to a collection multiple times?
What i want to do:
i am creating an event calendar and some events have multiple dates. To avoid having to add the event multiple times in the panel, i added a field where you can add all event dates.
The output of my calendar first shows the month (August 2024) and then all events in this period. So when an event series takes place in august and september, i want the event to be shown in both months.
Here is what i did:
$articles = $page
->children()
->listed()
->filterBy('template', 'event')
->filterBy('isEventSeries', false)
->filter(function ($child) {
$compareDate = mktime(0,0,0,date('m'),date('d'),date('Y'));
return $child->published()->toDate() <= time() && ( $child->eventdate()->toDate() >= $compareDate || $child->eventdateend()->toDate() >= $compareDate );
});
$articlesSeries = $page
->children()
->listed()
->filterBy('template', 'event')
->filterBy('isEventSeries', true)
->filter(function ($child) {
$compareDate = mktime(0,0,0,date('m'),date('d'),date('Y'));
return $child->published()->toDate() <= time() && ( $child->eventdate()->toDate() >= $compareDate || $child->eventdateend()->toDate() >= $compareDate );
});
foreach($articlesSeries as $articleItem) {
$months = [];
foreach($articleItem->eventSeriesList() as $eventDate) {
$month = $eventDate->seriesEventdate()->toDate('M');
if(in_array($month, $months) === false) {
array_push($months, $month);
$newItem = $articleItem->update([
'id' => $articleItem->title() . '-' . $month,
'title' => $articleItem->title() . '-' . $month,
'eventdate' => $eventDate->seriesEventdate()->toDate()
]);
$articles->add($newItem);
}
}
}
so basically i first get all events that ar not an event-series, then i collect all events which are and compare their dates from the list. for each month i wanted to add the item to my articles collection, but the event is only added once. I also tried to create a new page object which then should be added but the output stays the same. it only appears one time.
Im happy for all ideas. Thanks in advance!