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
This commit is contained in:
Flavio Copes 2015-12-29 19:10:30 +01:00
parent 3eb2a5664a
commit 017500a6b5

View File

@ -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.
*