Jump to content

Select page by last repeater item?


theoretic
 Share

Recommended Posts

Hi friends! And thanks for Processwire!

I have an interesting question concerning selectors. Let's suppose we have an "inventory" template with "owner" repeater holding all users who owned this inventory. The last repeater item represents the current owner. Is there any possibility to select an inventory page by some data contained in its last owner repeater item? Some pseudocode to clarify what i want:

//getting inventory recently owned by different users by currently owned by current user

$myInventoryPage = $pages->get("template=inventory,owners(last).id={$user->id}");

Using owners.id is not an option here because the "owner" repeater may contain the current user in any position, not the last one. Thanks in advance for any idea!

Link to comment
Share on other sites

I don't think you can do that in a selector, but you could add a hidden "owner" field to the "inventory" template and then populate that on page save with the value from the last repeater item.

So if "owners" is the repeater field and "owner" is a Page Reference field within the repeater that stores a user...

$pages->addHookAfter('saveReady', function(HookEvent $event) {
	/* @var Page $page */
	$page = $event->arguments(0);
	if($page->template == 'inventory') {
		$page->owner = $page->owners->last()->owner;
	}
});

Then you can easily find pages like this:

$myInventoryPage = $pages->get("template=inventory, owner=$user");

 

  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...