From 0d1c63f0fdfbdf2b9c1802895076a67cd5eb2ebd Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 18 Mar 2021 11:17:11 +0200 Subject: [PATCH] Fixed `Page::activeChild()` throwing an error [#3276] --- CHANGELOG.md | 1 + .../Types/Pages/Traits/PageRoutableTrait.php | 2 ++ system/src/Grav/Common/Page/Page.php | 22 ++++++++++--------- .../Flex/Pages/Traits/PageRoutableTrait.php | 18 +++++++-------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3f43daf9..a8d98b557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * Fixed broken media upload in `Flex` with `@self/path`, `@page` and `@theme` destinations [#3275](https://github.com/getgrav/grav/issues/3275) * Fixed media fields excluding newly deleted files before saving the object * Fixed method `$pages->find()` should never redirect [#3266](https://github.com/getgrav/grav/pull/3266) + * Fixed `Page::activeChild()` throwing an error [#3276](https://github.com/getgrav/grav/issues/3276) # v1.7.8 ## 03/17/2021 diff --git a/system/src/Grav/Common/Flex/Types/Pages/Traits/PageRoutableTrait.php b/system/src/Grav/Common/Flex/Types/Pages/Traits/PageRoutableTrait.php index 341ac4d27..3b490a596 100644 --- a/system/src/Grav/Common/Flex/Types/Pages/Traits/PageRoutableTrait.php +++ b/system/src/Grav/Common/Flex/Types/Pages/Traits/PageRoutableTrait.php @@ -15,6 +15,7 @@ use Grav\Common\Grav; use Grav\Common\Page\Interfaces\PageCollectionInterface; use Grav\Common\Page\Interfaces\PageInterface; use Grav\Common\Page\Pages; +use Grav\Common\Uri; use Grav\Common\Utils; use Grav\Framework\Filesystem\Filesystem; use RuntimeException; @@ -97,6 +98,7 @@ trait PageRoutableTrait public function activeChild(): bool { $grav = Grav::instance(); + /** @var Uri $uri */ $uri = $grav['uri']; /** @var Pages $pages */ $pages = $grav['pages']; diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index baac905f4..5c92cba9a 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -2496,21 +2496,23 @@ class Page implements PageInterface */ public function activeChild() { - $uri = Grav::instance()['uri']; - $pages = Grav::instance()['pages']; + $grav = Grav::instance(); + /** @var Uri $uri */ + $uri = $grav['uri']; + /** @var Pages $pages */ + $pages = $grav['pages']; $uri_path = rtrim(urldecode($uri->path()), '/'); - $routes = Grav::instance()['pages']->routes(); + $routes = $pages->routes(); if (isset($routes[$uri_path])) { + $page = $pages->find($uri->route()); /** @var PageInterface|null $child_page */ - $child_page = $pages->find($uri->route())->parent(); - if ($child_page) { - while (!$child_page->root()) { - if ($this->path() === $child_page->path()) { - return true; - } - $child_page = $child_page->parent(); + $child_page = $page ? $page->parent() : null; + while ($child_page && !$child_page->root()) { + if ($this->path() === $child_page->path()) { + return true; } + $child_page = $child_page->parent(); } } diff --git a/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php b/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php index b2be4ee1b..66b39556d 100644 --- a/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php +++ b/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php @@ -497,22 +497,22 @@ trait PageRoutableTrait public function activeChild(): bool { $grav = Grav::instance(); + /** @var Uri $uri */ $uri = $grav['uri']; + /** @var Pages $pages */ $pages = $grav['pages']; $uri_path = rtrim(urldecode($uri->path()), '/'); $routes = $pages->routes(); if (isset($routes[$uri_path])) { - /** @var PageInterface $child_page|null */ - $child_page = $pages->find($uri->route())->parent(); - if (null !== $child_page) { - while (!$child_page->root()) { - if ($this->path() === $child_page->path()) { - return true; - } - /** @var PageInterface $child_page|null */ - $child_page = $child_page->parent(); + $page = $pages->find($uri->route()); + /** @var PageInterface|null $child_page */ + $child_page = $page ? $page->parent() : null; + while ($child_page && !$child_page->root()) { + if ($this->path() === $child_page->path()) { + return true; } + $child_page = $child_page->parent(); } }