Querying for filtered pages using custom function in pages section

Hi there,

I want my events blueprint to have 2 pages sections:

One should show all of the past events and one should show all events that have dates in the future.

I have functions to get both of these collections. For example for the past events I do it like this:

function getPastEvents($events) {
  $pastEvents = [];
  $now = date("Y-m-d G:i");

  foreach($events as $event) {
    $allDatesPast = true;
    foreach($event->datestructure()->toStructure() as $date) {
      if($date->date()->toDate("Y-m-d G:i") >= $now) {
        $allDatesPast = false;
        break;
      }
    }
    if ($allDatesPast) {
      array_push($pastEvents, $event);
    }
  }

  return $pastEvents;
}

Now I want to display these events in the pages section. I guess it can be done with programmable blueprints, but I’m not sure how I can display my collection with it.

Thank you for your help!

Have you looked at the PagesDisplay Section plugin? If you need a filtered pages section in the blueprint, then it seems like this might help. You may still need to create a function in the page model or as a pageMethod in another plugin to do the actual filtering.

1 Like

Thank you @duncanmunro that led me on the right path.

This is how I did it now:
I created a pageModel under /site/models/events.php:

<?php

use Kirby\Cms\Page;
use Kirby\Cms\Pages;

  class EventsPage extends Page {
    public function getAllPastEvents() {
      $events = $this->children();
      $pastEvents = [];

      $now = date("Y-m-d G:i");

      foreach($events as $event) {
        $allDatesPast = true;
        foreach($event->datestructure()->toStructure() as $date) {
          if($date->date()->toDate("Y-m-d G:i") >= $now) {
            $allDatesPast = false;
            break;
          }
        }
        if ($allDatesPast) {
          array_push($pastEvents, $event);
        }
      }

      $pastEvents = new Pages($pastEvents);
      return $pastEvents;
    }

    public function getAllFutureEvents() {
      $events = $this->children();
      $futureEvents = [];

      $now = date("Y-m-d G:i");

      foreach($events as $event) {
        foreach($event->datestructure()->toStructure() as $date) {
          if($date->date()->toDate("Y-m-d G:i") >= $now) {
            array_push($futureEvents, $event);
          }
        }
      }

      $futureEvents = new Pages($futureEvents);
      return $futureEvents;
    }
  }

It’s essential to convert the array of pages into a Kirby\Cms\Pages object for querying.

Then in my blueprint I used a query to return the fetched pages like so:

        sections:
          pastevents:
            type: pagesdisplay
            query: site.find("events").getAllPastEvents
            layout: table
          futureevents:
            type: pagesdisplay
            query: site.find("events").getAllFutureEvents
            layout: table
1 Like