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.

Hello, I am struggling (as always…), I have a structure date in a block, and I want to sort the blocks by the date in the structure. Is that possible? Here are the blueprints and the
dateStructure I’m using (… :neutral_face:) Can you help me, thank you
cours.yml:

  cours:
    type: blocks
    pretty: true
    sortBy: dateStructure asc
    fieldsets:
      text:
        label: Liste des cours
        type: group
        fieldsets:
          - event

event.yml:

name: Cours
fields:
  […]
  calendrier:
    label: Date du cours ou du dernier cours!
    sortBy: date asc
    type: structure
    required: true
    fields:
      date:
        label: Jour(s)
        type: date
        width: 1/2
        default: today

index.php:

Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'dateStructure' => function () {
           if ( $item = $this->event()->calendrier()->toStructure()->sortBy( 'date', 'asc')->first() ) {
               return $item->date();
           }
        }
    ]
]);

I didn’t know there was a sortBy option for blocks? It somehow doesn’t even make sense.

And in any case, a page method definitely doesn’t make sense here.

1 Like

ok, thanks then