Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from moevbiz/master
Browse files Browse the repository at this point in the history
- Allow using pagesdisplay in user and file blueprints
- Kirby 3.9 compatibility
  • Loading branch information
rasteiner authored Dec 15, 2022
2 parents 07ad941 + 094eb80 commit 7ddc969
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 33 additions & 7 deletions src/PagesDisplaySection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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) {
Expand All @@ -29,16 +31,40 @@
'type' => fn() => 'pages',
],
'computed' => [
'parent' => function () {
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);
Expand Down

0 comments on commit 7ddc969

Please sign in to comment.