Query Date in Pages Section from Structure Field

Hi all!

I am setting up a site that lists events as pages that have each multiple dates. I want to display the events in the panel within a pages section (I use the Pagetable plugin but don’t think it makes a difference?). Here I would like to query the year of the first date of each event for an easier overview.

On the events page, I list each of the event’s dates in a structure field:

eventdates:
     type: structure
     label: Termine
     sortBy: eventdate asc
     fields:
          eventdate:
               type: date
               label: Termine
               display: DD.MM.YYYY
               time: true

On the parent page, I am trying to query the year of the first date from the structure:

sections:
     events:
          headline: " "
          type: pagetable
          columns:
               eventtitle:
                    label: Titel
                    text: "{{ page.title }}"
               firsteventdate:
                    label: Datum
                    text: "{{ page.eventdates.structureItem.eventdate('Y') }}"

Unfortunately, the last line doesn’t work. It does display all the structure items as text:

- eventdate: 2022-05-20 19:30:00 - eventdate: 2022-05-22 19:30:00 - eventdate: 2022-05-24 19:30:00

But I would like to simply have:

2022

I have tried all kinds of variants but nothing is working. Some of these produce nothing or 1970 for some reason:

page.eventdates.structureItem.eventdate('Y')
page.eventdates.structureItem.eventdate.toDate('Y')
page.eventdates.eventdate('Y')
page.eventdates.eventdate.toDate('Y')
page.eventdates.structureItem.first.eventdate.toDate('Y')
page.eventdates.structureItem.first.eventdate('Y')
page.eventdates.structureItem.eventdate.first.toDate('Y')

I am sure it’s possible but just a question of the right syntax? Can somebody help? :pray: Thanks in advance!

You have to create a page method (or page model method) that returns what you want, then use that in your query.

Thanks for the super quick answer! This got me on the right track :blush:

I found a helpful forum post that I built on:

Here’s the page model:

<?php
class EventPage extends Page {
  public function year() {
	return $this->eventdates()->toStructure()->first()->eventdate()->toDate("Y");
  }
}

And in the pages/Pagetable section, simply:

text: "{{ page.year }}"

Hooray! Thank you!