Jump to content

Change/Manipulate PageListNumChildren in Tree


Orkun
 Share

Recommended Posts

How can I change the NumChildren Label in the Tree?

post-3125-0-58397000-1450771944.png

I am logged in as a custom user. I have an SitehelperModule where I hook in the "ProcessPageList::execute" and execute and custom Function like this which excludes all pages which the current user can't edit:

And It works well, the current user gets only the Pages which he can edit, but the problem is, that the NumChridren shows that they are 5 Pages under the ParentPage and this is confusing for the customer. Can I manipulate that also? I am asking because since the PageListNumChildren is rendered by the ProcessPageList.js I dont know how to tell Processwire that it should only num the Pages which the current user can edit?

if(!$this->user->isSuperuser()) $this->addHookAfter('ProcessPageList::execute', $this, 'HidePages');
public function HidePages(HookEvent $event){

	// create an array with all id's of the page to exclude
	$uneditable_pages = new PageArray(); 
	$the_pages = wire('pages')->find("template=activity|location|organiser|address|source|tipp");
	foreach ($the_pages as $the_page) {
		if(!$the_page->editable()) {
			$uneditable_pages->add($the_page);
		}
	}
	$hidden = explode('|', $uneditable_pages);

	// make sure it's an ajax request
	if($this->config->ajax){
		// manipulate the json returned and remove any pages found from array
		$json = json_decode($event->return, true);
		foreach($json['children'] as $key => $child){
			if(in_array($child['id'],$hidden)) unset($json['children'][$key]);
		}
		$json['children'] = array_values($json['children']);
		$event->return = json_encode($json);
	}

}

This Code-Snippet is an mini-altered Version from Somas Code on Github

Link to comment
Share on other sites

As soon as it comes to pagination it does become difficult to know, which pages will be removed on runtime and which one aren't. That's why it's using numChildren, which just asks the mysql database. Mysql can count those pages without the performance issues that php might have. There's already a quite long github issue to the topic: https://github.com/ryancramerdesign/ProcessWire/issues/302

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...