mirror of
https://github.com/processwire/processwire.git
synced 2025-08-10 16:54:44 +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:
@@ -2409,7 +2409,9 @@ class Page extends WireData implements \Countable, WireMatchable {
|
||||
* Return this page’s parent pages, or the parent pages matching the given selector.
|
||||
*
|
||||
* This method returns all parents of this page, in order. If a selector is specified, they
|
||||
* will be filtered by the selector.
|
||||
* will be filtered by the selector. By default, parents are returned in breadcrumb order.
|
||||
* In 3.0.158+ if you specify boolean true for selector argument, then it will return parents
|
||||
* in reverse order (closest to furthest).
|
||||
*
|
||||
* ~~~~~
|
||||
* // Render breadcrumbs
|
||||
@@ -2421,11 +2423,15 @@ class Page extends WireData implements \Countable, WireMatchable {
|
||||
* // Return all parents, excluding the homepage
|
||||
* $parents = $page->parents("template!=home");
|
||||
* ~~~~~
|
||||
* ~~~~~
|
||||
* // Return parents in reverse order (closest to furthest, 3.0.158+)
|
||||
* $parents = $page->parents(true);
|
||||
* ~~~~~
|
||||
*
|
||||
* #pw-group-common
|
||||
* #pw-group-traversal
|
||||
*
|
||||
* @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 All parent pages, or those matching the given selector.
|
||||
*
|
||||
*/
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user