1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 18:24:57 +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

@@ -2409,7 +2409,9 @@ class Page extends WireData implements \Countable, WireMatchable {
* Return this pages parent pages, or the parent pages matching the given selector. * Return this pages 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 * 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 * // Render breadcrumbs
@@ -2421,11 +2423,15 @@ class Page extends WireData implements \Countable, WireMatchable {
* // Return all parents, excluding the homepage * // Return all parents, excluding the homepage
* $parents = $page->parents("template!=home"); * $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-common
* #pw-group-traversal * #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. * @return PageArray All parent pages, or those matching the given selector.
* *
*/ */

View File

@@ -160,18 +160,19 @@ class PageTraversal {
* Return this page's parent pages, or the parent pages matching the given selector. * Return this page's parent pages, or the parent pages matching the given selector.
* *
* @param Page $page * @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 * @return PageArray
* *
*/ */
public function parents(Page $page, $selector = '') { public function parents(Page $page, $selector = '') {
$parents = $page->wire('pages')->newPageArray(); $parents = $page->wire('pages')->newPageArray();
$parent = $page->parent(); $parent = $page->parent();
$method = $selector === true ? 'add' : 'prepend';
while($parent && $parent->id) { while($parent && $parent->id) {
$parents->prepend($parent); $parents->$method($parent);
$parent = $parent->parent(); $parent = $parent->parent();
} }
return strlen($selector) ? $parents->filter($selector) : $parents; return !is_bool($selector) && strlen($selector) ? $parents->filter($selector) : $parents;
} }
/** /**