Nukro

Change/Manipulate PageListNumChildren in Tree

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

Share this post


Link to post
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

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By tonicastillo
      Hi
      I need to change home page of my one page site. Not only the template, but also content.
      Actually page tree is like this:
      Actual home New home I would like to move "New home" to root, and delete "Actual home". I thank this would be easy to do, but I don't know how. Two templates allows to have children.
      Thanks a lot, (and excuse my english)
      I love PW!
      Toni Castillo
    • By regesh
      I have to languages - english and russian.
      I need russian language to be set as default for admin panel. How to do it? Thanks!
    • By EyeDentify
      Hello Fellow PW Fans and Gurus.

      I have run into a problem where i have created a template without a template file associated with it.
      Now i would like to HAVE a template file associated with it, not an alternate but as the main template file.

      So i thought, that's easy, I upload the template file and change it in template settings but I only get the Alternate template file setting?

      Ok so maybe i can change it via the API , so i wrote some code for this.
      <?PHP function changeTemplate($pages = null, $templates = null) { /* get the pages to change template on */ $logItems = $pages->findMany('template=tmp_log_item'); /* get the template object for our desired template */ $Template = $templates->get('tmp_new_log_item'); foreach($logItems AS $key => $logData) { /* use template object $Template to set template */ $logData->template = $Template; /* save page item */ $logData->save(); } } /* do the magic change */ changeTemplate(); ?> Needless to say, PW was not happy about this, It throw up an error message:
      Error: Exception: Invalid value sent to Page::setTemplate (in /home/virtual/mydomain/public_html/wire/core/Page.php line 1782)
      Now from what i understand i gave $logItem->template the wrong type of value.
      So what should the value be to correct this?
      When I read the docs at: https://processwire.com/api/ref/page/
      the value can be "string" or "Template".

      So i gave it a string and it went haywire.

      How should I approach this cause I realy dont want to manually change template file on över 50 plus pages

      Maybe I should use the Template class to create the value that the API wanted, but I am unsure how to use it.

      Thank you for any info you can give me on this.

      If you want more info to help me, ask and I will try my best to give it to you.
      Updated and Solved
      I solved the problem and have updated the code to reflect this.

      Essentialy i used the get() method of $templates to get the template object for my template i wanted to use using the template name.
      I gave the Object to the $logItem->template property and all went well.
      We learn something new every day.
      One Note:
      The template file you want to use must have been uploaded and added as a template in the Template section in the Admin for this code to work as expected.
    • By gebeer
      Hello,
      I read about conditional hooks and wanted to utilize them. In the process of trying to implement them I found that they do not get called.
      So I reduced my hook to a minimum
      wire()->addHookAfter('Page::changed', function($event) { die('changed'); }); And nothing happens at all. Also with Page::changed(title) or Page(template=basic-page)::changed(title) no luck.
      In the code comment in Wire.php it says: "Hookable method that is called whenever a property has changed while change tracking is enabled"
      I see that change tracking is enabled as
      wire()->addHookAfter('Pages::saveReady', function($event) { if($event->arguments[0]->isChanged('title')) die('changed'); }); is working.
      The hookable method ___changed() in Wire.php is empty.
      I tried this on 2 different installs PW 3.0.61 and 3.0.62
      Can anyone please confirm or let me know if I'm doing anything wrong here. Thank you.
    • By regesh
      Hi all.
      I need to change $config->paths->data['templates'] value from "templates" to "m_templates" for example.
      I think this wil be more easy to use specific folder for mobile view.
      How to change depending on some actions? (detecting if site viewing on mobile browser)
      Thanks