From 017500a6b5905618397704c52fdcf5e53c24e810 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Tue, 29 Dec 2015 19:10:30 +0100 Subject: [PATCH] Add a Page::topParent() method to retrieve the topmost parent of a page Adding to this branch as it's needed for the Admin change that handles this branch change --- system/src/Grav/Common/Page/Page.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index df750b9ab..f51914459 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -1716,6 +1716,31 @@ class Page return $pages->get($this->parent); } + /** + * Gets the top parent object for this page + * + * @return Page|null the top parent page object if it exists. + */ + public function topParent() + { + $topParent = $this->parent(); + + if (!$topParent) { + return null; + } + + while (true) { + $theParent = $topParent->parent(); + if ($theParent != null && $theParent->parent() !== null) { + $topParent = $theParent; + } else { + break; + } + } + + return $topParent; + } + /** * Returns children of this page. *