Get extreme value of a structure field column

Hi there,

I have a structure field with a date column. I need to extract the earliest of these dates. Is there any simple way to to this? I am still kind of new to PHP and Kirby.


Sort items by date and get the first item:

$structureItem = $page->structurefield()->toStructure()->sortBy('date', 'asc')->first();
echo $structureItem->date()->toDate('Y-m-d');

An alternative would be to pluck() all dates and then to use a PHP sort method to sort this array of dates and get the lowest.

On a side note, please always choose a question category that corresponds to your Kirby version. The above code won’t work with Kirby 2.

Ah, sorry, I was not aware that I can pick a question category. Will keep that in mind for next time.

And yes, this is indeed for a Kirby 2 project. Any way to get this to work with Kirby 2?

Yes, the change only affects the date method:

$structureItem = $page->structurefield()->toStructure()->sortBy('date', 'asc')->first();
echo $structureItem->date('Y-m-d');

Hm, this gives me an error Call to a member function date() on null on the second line.

I put it in like this:

<?php foreach ($pastShows as $show): ?>

  <!-- get premiere year -->

  <div class="tr">
    <p><?= $show->title()->html() ?></p>
        $structureItem = $show->events()->toStructure()->filterBy('date', 'asc')->first();
        echo $structureItem->date('Y');

<?php endforeach ?>

Am I doing something wrong?

Could you please post the blueprint with the structure field?

Here is the relevant part from the blueprint file:

    label: Aufführungen
    type: structure
    style: table
    sort: date
      - date
      - context
      - location
      - info
        label: Datum
        type: datetime
        modalsize: small
        label: Kontext
        type: text
        label: Spielstätte / Ort
        type: text
        label: Zusatzinfo
        type: text

@texnixe I have a feeling that my content files are messed up since I changed the blueprint files after I created them first and they contain elements of old fields. Having said that, they still all contain a date field in the structure field in question. But maybe this is still related to it not working.

Is there any way to cleanly re-create the content-files? Just saving them from the panel keeps the old fields.

Is not my day today, it should be sortBy(), not filterBy(), I’m really sorry for the confusion. Something happened to my brain or I’m doing too many things at the same time.

$structureItem = $page->events()->toStructure()->sortBy('date', 'asc')->first();
echo $structureItem->date('Y-m-d');

I have used this exact syntax now and also brought my content files in order, but I still get the error Call to a member function date() on null on the second line. Any idea what else could be wrong?

Maybe you are trying to get the field from the wrong page. Is $show really the page that has the events field?

Ok, I have it working now, using var_dump I was able to find out a page had an empty “events” field. What was weird is that I had all entries in my contents file. But they did not show up in the panel. So something seemed out of sync in inner workings of kirby. I readded the events by hand in the panel and saved which seemed to fix it. My git diff showed my that there were actually no changes at all to the contents file, so I wonder what was actually messed up here.

Generally, is there any way to cleanly recreate the content files from the panel after changing blue prints around? Because mine were messed up and that seemed to lead to some errors.

Either way, thanks a lot @texnixe for helping out and for staying patient with me.