maybe I understand something wrong in PHP (I’m not a programmer), so I always struggling with cases like this. Now, for example there is need to do a calendar of events on a page, and build it up from fields which are in structure fields in “project” pages (children of “projects” page). Here is one of my attempts, which doesn’t work properly (it make only one event).
There’s a second endforeach missing in both examples. Both versions are possible depending on whether or not you want to start a new list per project or not.
This solution return only one event to the list. So I’m still using the first one, which is almost good. But, there is one issue on that (which is maybe connected with your suggestion) – It looks like events are still together in projects. Because I have tried to sort it, and the items from particular project are still together. So, sorting works only “inside” projects and don’t affect whole list.
Thanks, I’m trying tu use debug mode, also var_dump. But the main issue remains – events are sorted only inside project. I understand that the nested foreach is probably making it, but I’m just looking if there is some easy way to prevent it with Kirby PHP API.
Last version is here
<ul>
<?php
foreach(page('projects')->children() as $projectPage):
foreach($projectPage->events()->toStructure()
->sortBy('date', 'asc')
->filter(function ($page) {return $page->date()->toDate() > time(); })
as $event): ?>
<li>
<span><?= $event->date()->toDate('j/n/y') ?></span>
<span><?= $event->category()->html() ?></span>
<span style="background-color:<?= $event->parent()->color()->html() ?>;">
<a href="<?= $event->parent()->url() ?>">
<?= $event->parent()->title()->html() ?>
</a>
</span>
<span><?= $event->venue()->html() ?></span>
</li>
<?php endforeach;
endforeach
?>
</ul>
I’ve been looked for some solution here on forum, but still have no idea.
For me, those nested arrays are pretty complicated and if there are not any easy solution in Kirby API, then maybe something is wrong in my structure, maybe in general. Would do I better if for example the events will store on other place in structure? Separated from project pages? Obviously, for my client it would be appropriate to add events in panel to corresponding Projects, but maybe I miss something …
Finally, I fixed this issue with some changes in structure. Events are subpages of their projects now. So, structure is maybe a little more complicated but call and list all events to calendars is much, much easier.
Thank you everyone for help.