Sum & count from grandchildren structure fields

Hi, I have one puzzle I just can’t solve, so any help is super appreciated.

Creating a Hippodrome racecourse events website, that has several levels of event structure.

– Events archive
----- Seasons
----------- Race days
----------------- Individual races as structure fields

Now, I am trying to count all events (from the structure field), and display them as numbers on the Season page and Events archive page.

I did it on the Season page

<?php foreach ($page->children()->listed() as $event) :

    $races = $event->races()->toStructure();
    $gallop = $races->filterBy('raceType', 'gallop');
    $trot = $races->filterBy('raceType', 'trot');
    $hurdle = $races->filterBy('raceType', 'hurdle');
    $racesCount = $races->count();
    $gallopCount = $gallop->count();
    $trotCount = $trot->count();
    $hurdleCount = $hurdle->count();

    ?>

But, how to sum them from all Season pages on the Events archive page?
Or count them from the Race days (grandchild)?

I‘d collect them all into a structure. There is an example somewhere here on the forum

1 Like

sorry for not anwering your question but consider this: from my experience more complex data is usually better of in pages than in structures. pages can have models and this methods to provide precalculated data. using a pages section with a info containing html provides a nice preview and sorting is possible. i know editing content is not as fast as with structures but easier processing and the posibility of status (draft, unlisted, listed) counter that well.
with a model even the draft status can be skipped and pages creates as unlisted/listed if thats needed.

1 Like

I really don’t know which is better, Kirby itself or your support on the forum. :pray:

Collecting in the structure was the most straightforward solution, and it works great. :sunglasses:

Here is the solution, if someone needs it in the future:

Created plugin as instructed in this thread, and created foreach loop with additional structure collection

<?php foreach ($page->children()->published()->sortBy('title', 'desc') as $season) :

        // fetch a collection of pages https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595
        $eventCollection = $season->children()->listed();

        // pass the collection of pages and the name of the field to the function https://k2.getkirby.com/docs/cookbook/the-structure-field#merging-structure-fields
        $entries = createNewStructure($eventCollection, 'races');

        // filter races
        $gallopRaces = $entries->filterBy('raceType', 'gallop');
        $trotRaces = $entries->filterBy('raceType', 'trot');
        $hurdleRaces = $entries->filterBy('raceType', 'hurdle');

        // count races
        $totalRaces = $entries->count();
        $totalGallop = $gallopRaces->count();
        $totalTrot = $trotRaces->count();
        $totalHurdle = $hurdleRaces->count();

    ?>

<?= $totalRaces ?>
<?= $totalGallop ?>
...

<?php endforeach ?>

This is something I am super interested in, but I still can’t fully understand the logic of page models, and possibilities. If you or anyone else don’t know the subject for some new Kirby cookbook article page models would be a great thing. I would even pay for articles or tutorials on that or similar advanced subjects.