Move PageModel to SiteMethod

So far I have a PageMethod that returns future events. It looks something like this:


  public function upcomingEvents() {
        $events = $this->children()->listed()->filter(function($child){
            return $child->hasUpcomingDate();
        })->sortBy('nearestDate', 'asc');

        return $events;
    }

I now need the filter directly on the overview panel, so moving it to a siteMethod seemed to make sense so that I can also filter the events here.

This is now part of the config. However, it does not seem to work. What could be the problem?


'siteMethods' => [
        'upcomingEvents' => function() {
            $events = kirby()->site()->children()->listed()->filter(function($child){
                return $child->hasUpcomingDate();
            })->sortBy('nearestDate', 'asc');

            return $events;
        }
    ]

Why not use a collection?

I’m not sure if I can access collections in a blueprint query. Is that possible?

Yes you can with…

query: kirby.collection("some-collection")

Excellent, it works wonderfully

1 Like

Just on a side note, you could have kept your model method and used it in the overview panel just as well. The model method works everywhere you use a page of the given page type.

The scope of the model is events, which does not exist in the overview panel. There are only individual events here. I therefore assumed that I cannot access the model because the context is missing, right?

Sorry, I’m missing the context where you are planning to use the method vs. where you had been using the method.