diff --git a/composer.json b/composer.json index 59ea1dd..94455e0 100755 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require": { - "getkirby/composer-installer": "^1.1" + "getkirby/composer-installer": "^1.2" }, "license": "ISC" } diff --git a/index.js b/index.js index b1b590a..895539a 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ -panel.plugin('rasteiner/k3-pagesdisplay-section', { +panel.plugin("rasteiner/k3-pagesdisplay-section", { components: { - 'k-pagesdisplay-section': { - extends: 'k-pages-section' - } - } + "k-pagesdisplay-section": { + extends: "k-pages-section", + }, + }, }); diff --git a/index.php b/index.php index b57ce68..00c463b 100644 --- a/index.php +++ b/index.php @@ -1,12 +1,7 @@ [ - 'pagesdisplay' => require __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'PagesDisplaySection.php' + 'pagesdisplay' => require __DIR__ . '/src/PagesDisplaySection.php' ] ]); \ No newline at end of file diff --git a/readme.md b/readme.md index abad627..5839f31 100644 --- a/readme.md +++ b/readme.md @@ -1,20 +1,41 @@ -Display any page list in a Section. Any parent, many parents, filtered, don't care. -On the other hand, you won't be able to sort the list or add new pages to it. +# Pages Display Section -# Install -## Download Zip file +Display any page list in a section using [Kirby's query language](https://getkirby.com/docs/guide/blueprints/query-language). Any parent, many parents, filtered, don't care. -Copy plugin folder into `site/plugins` +> ℹ️ Note: While this functionality gives you a lot of freedom, you won't be able to sort the list or add new pages to the query. -## Composer -Run `composer require rasteiner/k3-pagesdisplay-section`. +## Installation -# Usage -You select and filter the pages with the query language, with a `query` property in the section yaml. -You can start the query with `site`, `page` (refers to the current page), `pages` (which is equal to `site.pages`), or `kirby` (mainly to use `kirby.collection`). +### Download + +Download and copy this repository to `/site/plugins/k3-pagesdisplay-section`. + +### Git submodule + +```bash +git submodule add https://github.com/rasteiner/k3-pagesdisplay-section.git site/plugins/rasteiner/k3-pagesdisplay-section +``` + +### Composer + +```bash +composer require rasteiner/k3-pagesdisplay-section +``` + +## Usage + +Create a section of your liking and add a `query` property. Within the query you may select and filter any pages by making use of [Kirby's query language](https://getkirby.com/docs/guide/blueprints/query-language). + +You can start the query with one of the following variables: + +- `site` +- `page` (refers to the current page) +- `pages` (which equals `site.pages`) +- `kirby` (mainly to use with `kirby.collection`) ## Example -Show all pages that have "Foo" in their title: + +### All pages with `Foo` in their title ```yaml sections: @@ -24,8 +45,7 @@ sections: query: site.index.filterBy('title', '*=', 'Foo') ``` - -Show sibling pages (exclude current page): +## Sibling pages (exclude the current page) ```yaml sections: diff --git a/src/PagesDisplaySection.php b/src/PagesDisplaySection.php index 4233913..972c9f3 100755 --- a/src/PagesDisplaySection.php +++ b/src/PagesDisplaySection.php @@ -1,55 +1,70 @@ [ 'sortable' => function (bool $sortable = true) { return false; }, - 'query' => function(string $query = 'page.children') { + 'query' => function (string $query = 'page.children') { return $query; } ], 'computed' => [ - 'pages' => function () { + 'pages' => function () { + $kirby = kirby(); $q = new Query($this->query, [ - 'site' => site(), - 'page' => $this->model(), - 'pages' => site()->pages(), - 'kirby' => kirby() + 'kirby' => $kirby, + 'site' => $kirby->site(), + 'pages' => $kirby->site()->pages(), + 'page' => $this->model() ]); $pages = $q->result(); - if(is_a($pages, 'Kirby\\Cms\\Pages')) { - // show only readable - $pages = $pages->filterBy("isReadable", true); - - // sort - if ($this->sortBy) { - $pages = $pages->sortBy(...Str::split($this->sortBy, ' ')); - } - - // pagination - $pages = $pages->paginate([ - 'page' => $this->page, - 'limit' => $this->limit - ]); - - return $pages; - } else { + if (!is_a($pages, \Kirby\Cms\Pages::class)) { + $result = $pages === null ? 'null' : get_class($pages); throw new InvalidArgumentException( - 'Invalid query result - Result must be of type Kirby\\Cms\\Pages, ' - . ($pages === NULL ? 'NULL' : get_class($pages)) - . ' given.' + "Query result must be of type \"Kirby\\Cms\\Pages\", \"{$result}\" given" ); } + + // Loop for the best performance + foreach ($pages->data as $id => $page) { + // Remove all protected pages + if (!$page->isReadable()) { + unset($pages->data[$id]); + continue; + } + + // Filter by all set templates + if ($this->templates && !in_array($page->intendedTemplate()->name(), $this->templates)) { + unset($pages->data[$id]); + continue; + } + } + + // Sort pages + if ($this->sortBy) { + $pages = $pages->sort(...$pages::sortArgs($this->sortBy)); + } + + // Flip pages + if ($this->flip) { + $pages = $pages->flip(); + } + + // Add pagination + $pages = $pages->paginate([ + 'page' => $this->page, + 'limit' => $this->limit + ]); + + return $pages; }, 'add' => function () { return false; @@ -57,5 +72,7 @@ 'sortable' => function () { return false; } - ], -]); + ] +]; + +return array_replace_recursive($base, $extension);