Calling item from structure inside YAML

I have this structure type within a blueprint called event.yml

          datetime:
            label: Event Details
            type: structure
            fields:
              date:
                type: date
                time: false
              open:
                label: Open Time
                type: time
                notation: 12
                width: 1/3
              close: 
                label: Close Time
                type: time
                notation: 12
                width: 1/3

I’m trying to sort events on the parent page (events.yml) by the date that is within this datetime structure like so:

type: pages

headline: Annual Events

parent: kirby.page("events")

info: "{{ page.datetime.date}}"

template: event

empty: No events yet

sortBy: page.datetime

but I’m not entirely sure how to call the date that’s been listed within the structure inside of YAML. Can someone tell me if that’s possible?

You would need a custom page method here, that returns the date (first/last?) you want to sort by from the structure field.

It would be sorted by earliest to latest (unless there could be something like it’s sorted by whichever one is coming up next)

I’ve never made a custom page method before, how would I do something like that?

Docs: https://getkirby.com/docs/reference/plugins/extensions/page-methods

I’m just getting back to this since this is lower on the totem pole for my project. Is there an example of how something like this would then be called in yaml?

Well, the method itself will be called just like any other method in your blueprints, so sortBy: page.mycustomMethod.

Your method would have to return a single date to sort by, either the lowest/highest open or the lowest/highest close date.

So I’ve got this now!

Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'dateStructure' => function () {
            foreach($this->datetime()->toStructure() as $item): 
                return $item->date();
            endforeach; 
        }
    ]
]);

Which is now showing the correct date on the panel but when I go to do sortBy: page.dateStructure it still doesn’t do anything. What am I missing?

    'pageMethods' => [
        'dateStructure' => function () {
           if ( $item = $this->datetime()->toStructure()->sortBy( 'date', 'asc')->first() ) {
               return $item->date();
           }
     
        }
    ],

In blueprint:

sortBy: dateStructure desc

Oh wow, okay so the sorting had to happen within the page method? Alright, I was confused as to whether it had to happen in there or I just had to get the proper date and then sortBy in YAML would do the rest.

Will test this out soon and get back to you here.