Jump to content

Filtering pages from the Pages tree and displaying the correct number of children


Recommended Posts

Posted (edited)

I'm looking for a solution that would allow me to hide some templates from the tree list in the admin and modify the Children number to the correct value.
Is there such a hook?
I would also like it to work for superuser.

I tried couple of them like ProcessPageList::find or ProcessPageListRenderJSON::execute but without luck.

29263.png.daf98aa1987f41aca9e7d1499cc7ac15.png

 

Update:

I managed to achieve what I wanted, but to do it I had to use two separate hooks.

$wire->addHookAfter('ProcessPageListRender::getNumChildren', function (HookEvent $event) {
   
    $user = $event->wire()->user;
    $username = $user->name;

    if ($username !== FULL_PAGE_TREE_USERNAME) {
        $page = $event->arguments(0);
        $return = $event->return;
        $return = $page->numChildren('template!=category|board-category, include=all');
        $event->return = $return;
    }

});
$wire->addHookAfter('ProcessPageList::find', function (HookEvent $event) {
    // Get the user
    $user = $event->wire()->user;
    $username = $user->name;

    if ($username !== FULL_PAGE_TREE_USERNAME) {
        $event->return->each(function ($p) use ($event) {
            if ($p->template == 'material-group')
                $event->return->remove($p);
            if ($p->template == 'board-category')
                $event->return->remove($p);
            if ($p->template == 'category')
                $event->return->remove($p);
            return;
        });
    }
});

I hope it doesn't have any side effects. If there's a better method, I'd love to know about it 😉

Edited by PawelGIX
Working solution
Posted

After adding to ready.php this code nothing was logged.
 

$wire->addHookBefore('ProcessPageLister::getSelector', function(HookEvent $event) {
  // Get the object the event occurred on, if needed
  $ProcessPageLister = $event->object;
  bd($event, '$arguments');
});

 

Posted (edited)

It works but does not affect the number of children displayed.
So there is a discrepancy between what you see in the tree and the number next to it.

Edited by PawelGIX
Posted

Here is what I did - or rather what AI did for me... I don't know if it's better or worse than your code though.

Spoiler
# ================================================================================
# PAGE TREE: HIDE ALL PAGES FROM CLIENT
# WHERE THE PAGE TEMPLATE STARTS "PB-"
# ================================================================================

$this->addHookAfter('ProcessPageList::find', function (HookEvent $event) {
	if ($this->wire('user')->isSuperuser()) {
		return; // Don't execute for superusers
	}

	$pbTemplates = $this->wire('templates')->find("name^=pb-");
	$pbTemplateNames = [];
	foreach ($pbTemplates as $template) {
		$pbTemplateNames[] = $template->name;
	}

	if (!empty($pbTemplateNames)) {
		$event->return->find([
			"template" => $pbTemplateNames, // Pages with 'pb-' templates
		])->each(function($p) use ($event) {
			$event->return->remove($p);
		});
	}
});

# ================================================================================
# PAGE TREE: RECALCULATE # OF PAGES PER BRANCH
# SO THAT # DISPLAYED IS CORRECT, AND DROPDOWN
# CHEVRONS ONLY APPEAR WHEN NECESSARY
# ================================================================================

$wire->addHookBefore('ProcessPageListRender::getNumChildren', function(HookEvent $event) {
	if ($this->wire('user')->isSuperuser()) {
		return; // Don't execute for superusers
	}

	$allTemplates = $this->wire('templates');
	$pbTemplates = $this->wire('templates')->find("name^=pb-");
	$pbTemplateNames = [];
	foreach ($pbTemplates as $template) {
		$pbTemplateNames[] = $template->name;
	}

	$allTemplateNames = [];
	foreach ($allTemplates as $template) {
		$allTemplateNames[] = $template->name;
	}

	$allowed_templates = array_diff($allTemplateNames, $pbTemplateNames);

	$event->wire;
	$selector = 'template=' . implode('|', $allowed_templates);
	$event->arguments(1, $selector);
});

 

 

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.
×
×
  • Create New...