Jump to content
Orkun

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 horst
      Change Default Language to be None-English | Walk Trough
      When you start a new (single) language site and the default language shouldn't be English, you can change it this way:
      Go to the modules core section:

       
      Select the Language ones by the filter function:

       
      We have four language related modules here, but for a single language site in none english,
      we only need the base module, named "Languages Support". So go on and install it.

       
      After that, you can leave it, ... 

       
      ... and switch to the newly created Language section under SETUP:

       
      Select the default language

       
      Enter your new language name or its Shortcut and save the page.
      I will use DE for a single language site in german here as example:

       
      Now I go to the ProcessWire online modules directory, down to the subsection for language packs and select
      and download my desired (german) one: 

       

       

       

       
      After downloading a lang pack as ZIP, I go back into my SETUP > LANGUAGES > default language page in admin,
      select the downloaded lang pack ZIP and install it:

       

       

       
       
      After the ZIP is uploaded, the files are extracted and installed, most of my screen is already in
      the new default language. To get all fully switched, we save and leave that page, ...

       
      ... and completely logout from the admin.

       
      Now, of course, we directly login back, ...

      ... and see, that now also the cached parts of the admin have switched to the new default language. 🙂

       
      That was it for a single language site in none english.
       
      If you want to have a multi language site, just add more languages to the SETUP > LANGUAGES section.
      When using a multi language site, I think you also want to use multi language input fields, and maybe different page names for your language page pendents. If so, you need to go into MODULES > CORE > filter LANGUAGE and install what you need or want to use of it, (if not already done).
      Thanks for reading and happy coding, 🙂
       
    • By gerritvanaaken
      My client could save some time if she could select multiple pages at once. Of yourse, we are using the standard page selector in multi-tree mode, but she has to select one page after another. Each time she has to open the site tree again and open the required subtree, finding the page she wants to select.
      Would be cool to let the tree open after selecting a page, so she could select multiple pages in one "tree session". Understand what I mean? Anybody any solution to this?
      Alternatively, maybe the site tree could keep its state with all the subtrees open, when you are re-open it again.
    • 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 $logData->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 $logData->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 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!
×
×
  • Create New...