rapha
September 12, 2024, 8:28am
1
Hi all,
I try to filter and sorting Publications/Events from a Structured field (eventdates) value named eventdate. this is part of my controller code in which i get all publications and try to filter the events out.
Controller Snippet
$eventFilter = 'Veranstaltungen';
$allEvents = $publications->filterBy('datafilter', $eventFilter);
$events = $allEvents->filter(function ($child) {
return $child->eventDates()->toStructure()->filter(function ($item) {
return $item->eventdate()->toDate() >= time();
});
})->limit(3);
Blueprint
eventDates:
label: Veranstaltungs Daten
type: structure
sortBy: eventDate desc
fields:
eventDate:
label: Datum
type: date
width: 1/3
display: dd.mm.yyyy
startTime:
label: Startzeit
type: time
width: 1/3
endTime:
label: Endzeit
type: time
width: 1/3
Events have multiple Dates (eventdate) stored in Structured field.
I want to achief getting the nearest 3 future events from today.
thank you for your hints.
cheers raphael
texnixe
September 12, 2024, 10:06am
2
You would have to fetch all items from all pages into one structure collection first. Then do the filtering on that collection.
rapha
September 12, 2024, 1:24pm
3
thank you @texnixe
sorry for the silly question but how do i make a structure collection? I just know the “normal” collections under site/collections .
rapha
September 12, 2024, 1:57pm
4
i am not sure but i think i got it with:
$allEvents = $publicationsPage->children()->filterBy('datafilter', 'Veranstaltungen');
$allEntries = new Structure();
foreach ($allEvents as $event) {
$allEntries->add($event->eventDates()->toStructure());
}
$upcoming = $allEntries->filter(function ($child) {
return $child->eventdate()->toDate() > time();
});
$upcoming->sortBy('eventdate', 'desc');
i get now:
0 => Kirby\Cms\StructureObject {#1957 ▶}
1 => Kirby\Cms\StructureObject {#1959 ▶}
2 => Kirby\Cms\StructureObject {#1961 ▼
#field: Kirby\Content\Field {#1955 ▶}
#id: "2"
#params: array:10 [▶]
#parent: Kirby\Cms\Page {#414 ▶}
#siblings: Kirby\Cms\Structure {#1956}
#content: Kirby\Content\Content {#1962 ▶}
how do i access the content of these?
rapha
September 12, 2024, 3:01pm
5
Ok my final collections is like:
<?php
use \Kirby\Cms\Structure;
return function () {
$publicationsPage = site()->sitedefaults()->toObject()->publicationPage()->toPage();
$allEvents = $publicationsPage->children()->filterBy('datafilter', 'Veranstaltungen');
$allEntries = new Structure();
foreach ($allEvents as $event) {
$allEntries->add($event->eventDates()->toStructure());
}
$upcoming = $allEntries->filter(function ($child) {
return $child->eventdate()->toDate() > time();
})->sortBy('eventdate', 'asc')->limit(3);
return $upcoming;
};
and this seams to work very well. get the 3 next upcoming events.