1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-18 04:22:10 +02:00

Add support for $page->parents(true); which returns parents in reverse order (closest to furthest) rather than breadcrumb order.

This commit is contained in:
Ryan Cramer
2020-05-29 14:14:30 -04:00
parent 1af391f4db
commit 9add4e5f82
2 changed files with 12 additions and 5 deletions

View File

@@ -160,18 +160,19 @@ class PageTraversal {
* Return this page's parent pages, or the parent pages matching the given selector.
*
* @param Page $page
* @param string|array $selector Optional selector string to filter parents by
* @param string|array|bool $selector Optional selector string to filter parents by or boolean true for reverse order
* @return PageArray
*
*/
public function parents(Page $page, $selector = '') {
$parents = $page->wire('pages')->newPageArray();
$parent = $page->parent();
$method = $selector === true ? 'add' : 'prepend';
while($parent && $parent->id) {
$parents->prepend($parent);
$parents->$method($parent);
$parent = $parent->parent();
}
return strlen($selector) ? $parents->filter($selector) : $parents;
return !is_bool($selector) && strlen($selector) ? $parents->filter($selector) : $parents;
}
/**