From 094eb8091f6fb99116c378884efa434ecf3dba46 Mon Sep 17 00:00:00 2001 From: Roman Steiner Date: Thu, 15 Dec 2022 14:25:51 +0100 Subject: [PATCH] k3.9 compat, more starting vars --- composer.json | 2 +- src/PagesDisplaySection.php | 49 +++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index f2eb9fc..494d745 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "rasteiner/k3-pagesdisplay-section", "description": "K3 plugin: display any page list in a section. Any parent, many parents, filtered, don't care.", "type": "kirby-plugin", - "version": "0.2.3", + "version": "0.2.4", "authors": [ { "name": "Roman Steiner", diff --git a/src/PagesDisplaySection.php b/src/PagesDisplaySection.php index 80f967f..8fbd3f2 100755 --- a/src/PagesDisplaySection.php +++ b/src/PagesDisplaySection.php @@ -2,7 +2,9 @@ use Kirby\Exception\InvalidArgumentException; use Kirby\Cms\Section; -use Kirby\Toolkit\Query; +use Kirby\Cms\Page; +use Kirby\Cms\Site; +use Kirby\Cms\User; $base = Section::$types['pages']; @@ -15,7 +17,7 @@ 'sortable' => function (bool $sortable = true) { return false; }, - 'query' => function (string $query = 'page.children') { + 'query' => function (string $query = null) { return $query; }, 'controls' => function ($controls = true) { @@ -30,28 +32,39 @@ ], 'computed' => [ 'parent' => function () { - $parent = $this->parentModel(); - - if ( - $parent instanceof \Kirby\Cms\Site === false && - $parent instanceof \Kirby\Cms\Page === false && - $this->query === 'page.children' - ) { - throw new InvalidArgumentException("You must provide a query when using pagesdisplay in a user or file blueprint."); - } - - return $parent; + return $this->parentModel(); }, 'pages' => function () { + $model = $this->parentModel(); + + $query = $this->query ?? match(true) { + is_a($model, Site::class) => 'pages', + is_a($model, User::class) => 'pages', + default => 'page.children', + }; + $kirby = kirby(); - $q = new Query($this->query, [ + $isPageOrSite = is_a($model, Page::class) || is_a($model, Site::class); + $context = [ 'kirby' => $kirby, 'site' => $kirby->site(), 'pages' => $kirby->site()->pages(), - 'page' => $this->model() - ]); - - $pages = $q->result(); + 'model' => $model, + 'page' => $isPageOrSite ? $model : $model->parent(), + 'user' => $isPageOrSite ? null : $model, + 'file' => $isPageOrSite ? null : $model, + ]; + + $pages = null; + + // check if Kirby\Query\Query class exists (new in 3.8) + if (class_exists('Kirby\\Query\\Query')) { + $q = new Kirby\Query\Query($query); + $pages = $q->resolve($context); + } else { + $q = new Kirby\Toolkit\Query($query, $context); + $pages = $q->result(); + } if (!is_a($pages, \Kirby\Cms\Pages::class)) { $result = $pages === null ? 'null' : get_class($pages);