1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-18 20:41:16 +02:00

Add ProcessPageListRender::getNumChildren() hook per issue/request processwire/processwire-issues#649

This commit is contained in:
Ryan Cramer
2019-02-26 08:39:29 -05:00
parent 706dd519af
commit 9a75980f8d
3 changed files with 49 additions and 9 deletions

View File

@@ -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";
}

View File

@@ -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);
}
}

View File

@@ -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);