Jump to content

ProcessPageList Hook doesn't work 100% in PW 3


Orkun
 Share

Recommended Posts

Hi fellows

I have a problem with one of my hooks in processwire 3. It's a hook which manipulates the processpagelist to only show the pages which are not editable and not addable. This works fine in 2.7.3 but in pw 3 it doesn't work 100%. When the first time the ProcessPageList is rendered the hook works. Now when I open a branch and reload then it shows all pages. but when i close the branch before reload it works again and only shows the pages which are editable or addable.

 

How it should look like:

newsletter folders.png

 

How it looks when i let the branch open and reload:

newsletter folders showing.png

 

PHP-Code

$this->addHookAfter('ProcessPageList::execute', $this, 'hidePages');

	public function hidePages(HookEvent $event){
		$excludePages = new PageArray();
		$the_pages = wire('pages')->find("template=newsletter-clinic|sender_2016|sender_2016_collab|inside-news-folder|event-clinic");
		foreach ($the_pages as $the_page) {
			if(!$the_page->editable() && !$the_page->addable()) {
			  $excludePages->add($the_page);
			}
		}
		$hidden = explode('|', $excludePages);

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

 

Do you perhaps know what causes this and how to fix this?

Link to comment
Share on other sites

Not certain, but when I hook into ProcessPageList::execute I use AddHookBefore and it seems to work fine. The other thing that might be involved is the ajax check you have. In PW 3, the page tree is cached, so perhaps that ajax check is returning false, so it's not modifying what is returned?

Link to comment
Share on other sites

6 minutes ago, adrian said:

Not certain, but when I hook into ProcessPageList::execute I use AddHookBefore and it seems to work fine. The other thing that might be involved is the ajax check you have. In PW 3, the page tree is cached, so perhaps that ajax check is returning false, so it's not modifying what is returned?

Thanks for your answer @adrian

I tried it with addHookBefore and now it doesn't work at all. How does your code looks like? Are you also using the code from Soma

Didn't knew that PW 3 is caching the Page Tree perhaps you are right about the ajax check  ???.

I have now tried an another method which ryan mentioned here. This works now so far I can tell, but it is much much slower than the json method.

$this->addHookAfter("Page::listable", $this, "hookListable");

// getPacesToExclude() function returns the pages i want to hide
public function hookListable(HookEvent $event){
	 $page = $event->object;
	 if($this->getPagesToExclude()->has($page)){
	      $event->return = false;
	 }
}

 

Link to comment
Share on other sites

Looking into it a little more, I have done both before and after in different scenarios:

https://gist.github.com/adrianbj/6fd1b770d9ce7a7252b6

https://gist.github.com/adrianbj/e391e2e343c5620d0720

https://github.com/adrianbj/AdminRestrictBranch/blob/master/AdminRestrictBranch.module#L108

The first two I haven't used in a long time, but the last one I use regularly, but it's not manipulating the same way you are looking for. Perhaps you could see if one of the first two works for you though in 3.x - sorry I don't have more time right now to help debug.

  • Like 1
Link to comment
Share on other sites

On 16.9.2016 at 3:17 PM, adrian said:

The other thing that might be involved is the ajax check you have. In PW 3, the page tree is cached, so perhaps that ajax check is returning false, so it's not modifying what is returned?

Your assumption is right.

I tested it and logged the ajax calls in the pagetree:

At the first I'm opening some branches inside the page. It has made some logs since the pagetree is called from ajax:

ajaxtreecall 1.png

 

But after refresh and navigating (the branches are still in open state) to other pages and back to tree, the log file hasn't changed since it hasn't occured an ajax call (tree cached like you said) . So the hook doesn't return the altered pagetree since the statement is false.

ajaxtreecall 2.png

Therefore I think the 2 gist codes you linked will probably not work since it is also called inside an ajax-check. I will give a look at your module, perhaps I can make it work with it.

 

Greetings Orkun

 

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...