From 9a75980f8d8d3f2e0def4736c8b5d9a11b0c955a Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Tue, 26 Feb 2019 08:39:29 -0500 Subject: [PATCH] Add ProcessPageListRender::getNumChildren() hook per issue/request processwire/processwire-issues#649 --- .../ProcessPageList/ProcessPageList.module | 14 ++++--- .../ProcessPageList/ProcessPageListRender.php | 40 ++++++++++++++++++- .../ProcessPageListRenderJSON.php | 4 +- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/wire/modules/Process/ProcessPageList/ProcessPageList.module b/wire/modules/Process/ProcessPageList/ProcessPageList.module index d73370ab..f95877c7 100644 --- a/wire/modules/Process/ProcessPageList/ProcessPageList.module +++ b/wire/modules/Process/ProcessPageList/ProcessPageList.module @@ -8,7 +8,7 @@ * For more details about how Process modules work, please see: * /wire/core/Process.php * - * ProcessWire 3.x, Copyright 2018 by Ryan Cramer + * ProcessWire 3.x, Copyright 2019 by Ryan Cramer * https://processwire.com * * @property bool $showRootPage Whether root page (like home) should be shown. @@ -24,6 +24,8 @@ * * @method string ajaxAction($action) * @method PageArray find($selectorString, Page $page) + * + * @todo Option to configure whether "Pub" action should appear for non-superusers * */ @@ -194,12 +196,12 @@ class ProcessPageList extends Process implements ConfigurableModule { } $p = $this->wire('page'); - if($p->name == 'list' && $p->process == $this) { + if($p->name == 'list' && "$p->process" == "$this") { // ensure that we use the page's title is always consistent in the admin (i.e. 'Pages' not 'Page List') $p->title = $p->parent->title; } - if($ajax && $this->id > 1 && $p->process == $this && $this->wire('input')->get('mode') != 'select') { + if($ajax && $this->id > 1 && "$p->process" == "$this" && $this->wire('input')->get('mode') != 'select') { // remember last requested id $this->wire('session')->setFor($this, 'lastID', $this->id); } @@ -262,7 +264,7 @@ class ProcessPageList extends Process implements ConfigurableModule { foreach($this->openPage->parents() as $parent) { if($parent->id > 1 && $parent->id != $this->id) $openPageIDs[] = $parent->id; } - } else if(!$isAjax && $this->wire('page')->process == $this) { + } else if(!$isAjax && ((string) $this->wire('page')->process) == "$this") { if($this->id) { // leave openPageIDs as empty array } else { @@ -545,7 +547,7 @@ class ProcessPageList extends Process implements ConfigurableModule { $url = $page->url(); } - $numChildren = $id > 1 ? $page->numChildren : 0; + $numChildren = $id > 1 ? $renderer->numChildren($page) : 0; $label = $renderer->getPageLabel($page, array('noTags' => true, 'noIcon' => true)); if(strlen($label) > $maxLabelLength) { $label = substr($label, 0, $maxLabelLength); @@ -573,7 +575,7 @@ class ProcessPageList extends Process implements ConfigurableModule { 'edit' => $editable ); - if($page->id > 1 && $page->numChildren) { + if($numChildren) { $a['navJSON'] = $data['url'] . "?parent_id=$page->id"; } diff --git a/wire/modules/Process/ProcessPageList/ProcessPageListRender.php b/wire/modules/Process/ProcessPageList/ProcessPageListRender.php index 4506e58c..07001fe9 100644 --- a/wire/modules/Process/ProcessPageList/ProcessPageListRender.php +++ b/wire/modules/Process/ProcessPageList/ProcessPageListRender.php @@ -5,6 +5,7 @@ * * @method array getPageActions(Page $page) * @method string getPageLabel(Page $page, array $options = array()) + * @method int getNumChildren(Page $page, $selector = null) For hooks only, do not call directly * */ abstract class ProcessPageListRender extends Wire { @@ -21,6 +22,7 @@ abstract class ProcessPageListRender extends Wire { protected $options = array(); protected $useTrash = false; protected $qtyType = ''; + protected $numChildrenHook = false; // is ProcessPageListRender::numChildren() hooked? public function __construct(Page $page, PageArray $children) { $this->page = $page; @@ -46,6 +48,7 @@ abstract class ProcessPageListRender extends Wire { require_once(dirname(__FILE__) . '/ProcessPageListActions.php'); $this->actions = $this->wire(new ProcessPageListActions()); $this->actions->setActionLabels($this->actionLabels); + $this->numChildrenHook = $this->wire('hooks')->isMethodHooked($this, 'getNumChildren'); } public function setOption($key, $value) { @@ -207,7 +210,7 @@ abstract class ProcessPageListRender extends Wire { } public function getMoreURL() { - if($this->limit && ($this->page->numChildren(1) > ($this->start + $this->limit))) { + if($this->limit && ($this->numChildren($this->page, 1) > ($this->start + $this->limit))) { $start = $this->start + $this->limit; return $this->config->urls->admin . "page/list/?&id={$this->page->id}&start=$start&render=" . $this->getRenderName(); } @@ -222,5 +225,40 @@ abstract class ProcessPageListRender extends Wire { return $this->useTrash; } + /** + * Hook this method if you want to manipulate the numChildren count for pages + * + * ~~~~~ + * $wire->addHookAfter('ProcessPageListRender::getNumChildren', function($event) { + * $page = $event->arguments(0); + * $selector = $event->arguments(1); + * $event->return = $page->numChildren($selector); // your implementation here + * }); + * ~~~~~ + * + * See Page::numChildren() for details on arguments + * + * #pw-hooker + * + * @param Page $page + * @param string|int|bool|null $selector + * @return int + * + */ + protected function ___getNumChildren(Page $page, $selector = null) { + return $page->numChildren($selector); + } + + /** + * Return number of children for page + * @param Page $page + * @param string|int|bool|null $selector + * @return int + * + */ + public function numChildren(Page $page, $selector = null) { + return $this->numChildrenHook ? $this->getNumChildren($page, $selector) : $page->numChildren($selector); + } + } diff --git a/wire/modules/Process/ProcessPageList/ProcessPageListRenderJSON.php b/wire/modules/Process/ProcessPageList/ProcessPageListRenderJSON.php index 8afb9007..c3d20e9a 100644 --- a/wire/modules/Process/ProcessPageList/ProcessPageListRenderJSON.php +++ b/wire/modules/Process/ProcessPageList/ProcessPageListRenderJSON.php @@ -64,7 +64,7 @@ class ProcessPageListRenderJSON extends ProcessPageListRender { $note = "< " . $this->_("Trash open: drag pages below here to trash them"); // Message that appears next to the Trash page when open } $icons = array('trash-o'); // override any other icons - $numChildren = $page->numChildren(false); + $numChildren = $this->numChildren($page, false); if($numChildren > 0 && !$this->superuser) { // manually count quantity that are listable in the trash $numChildren = 0; @@ -81,7 +81,7 @@ class ProcessPageListRenderJSON extends ProcessPageListRender { if($page->hasStatus(Page::statusTemp)) $icons[] = 'bolt'; if($page->hasStatus(Page::statusLocked)) $icons[] = 'lock'; if($page->hasStatus(Page::statusDraft)) $icons[] = 'paperclip'; - $numChildren = $page->numChildren(1); + $numChildren = $this->numChildren($page, 1); $numTotal = strpos($this->qtyType, 'total') !== false ? $page->numDescendants : $numChildren; } if(!$label) $label = $this->getPageLabel($page);