From b56ede81f0ee964cd41116629d2c019d543a0035 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 7 Nov 2019 13:14:51 +0200 Subject: [PATCH] Flex Pages: Implemented recursive filterBy() --- .../src/Grav/Common/Page/Flex/PageIndex.php | 2 +- .../src/Grav/Common/Page/Flex/PageObject.php | 26 +++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/system/src/Grav/Common/Page/Flex/PageIndex.php b/system/src/Grav/Common/Page/Flex/PageIndex.php index c486a6a40..260c5db22 100644 --- a/system/src/Grav/Common/Page/Flex/PageIndex.php +++ b/system/src/Grav/Common/Page/Flex/PageIndex.php @@ -267,7 +267,7 @@ class PageIndex extends FlexPageIndex foreach ($children as $child) { $selected = $child->path() === $extra; $includeChildren = \is_array($leaf) && !empty($leaf) && $selected; - if (!$selected && !$child->filterBy($filters)) { + if (!$selected && !$child->filterBy($filters, true)) { continue; } if ($field) { diff --git a/system/src/Grav/Common/Page/Flex/PageObject.php b/system/src/Grav/Common/Page/Flex/PageObject.php index 1df20d740..36527a9fa 100644 --- a/system/src/Grav/Common/Page/Flex/PageObject.php +++ b/system/src/Grav/Common/Page/Flex/PageObject.php @@ -256,9 +256,10 @@ class PageObject extends FlexPageObject * - translated: bool * * @param array $filters + * @param bool $recursive * @return bool */ - public function filterBy(array $filters): bool + public function filterBy(array $filters, bool $recursive = false): bool { foreach ($filters as $key => $value) { if ($value === null || $value === '') { @@ -268,22 +269,23 @@ class PageObject extends FlexPageObject case 'search': $matches = $this->search((string)$value); break; - case 'page_type': // filename - $matches = true; + case 'page_type': + $types = $value ? explode(',', $value) : []; + $matches = in_array($this->template(), $types, true); break; - case 'extension': // .en.md + case 'extension': $matches = Utils::contains((string)$value, $this->extension()); break; - case 'modular': // _name (filename) - case 'visible': // order + case 'modular': + case 'visible': case 'routable': case 'published': $matches = $this->{$key}() === (bool)$value; break; - case 'page': // markdown in index + case 'page': $matches = $this->isPage() === (bool)$value; break; - case 'translated': // markdown in index + case 'translated': $matches = $this->hasTranslation() === (bool)$value; break; default: @@ -291,6 +293,14 @@ class PageObject extends FlexPageObject break; } if ($matches === false) { + if ($recursive) { + foreach ($this->children() as $child) { + if ($child->filterBy($filters, true)) { + return true; + } + } + } + return false; } }