Jump to content

Getting repeater items directly


DrQuincy
 Share

Recommended Posts

I've got a repeater of product options within a single product page template.

So template=product has a template file and has a field called options, which is a repeater with title and price.

I want to store the repeater IDs in a session for the shopping basket and then read them. E.g. if ID 1255 is in the array, this gets me the repeater item:

$item = $this->wire('pages')->get('template=product, options.id=1255')->options->get('id=1255');
  1. Is there a way to access the repeater directly? It just seems a bit clunky and inefficient to have to get the page and then the repeater by expressing the ID twice, when the ID should be able to get me to the repeater item with one call.
  2. I can run $pages->get(123) with a top-level page and it gets the page but when using get() from a repeater item I have to do get('id=123') — any reason for this? It doesn't really matter, I just wondered.
  3. Is it possible to have a Page Reference field that references a repeater for that page only? So, say I have options in a repeater as above: (Small, £10), (Large, £15), could I have a Page Reference field that let's me pick Small, Large, etc? The use case is specifying product bundles.

Thanks!

Link to comment
Share on other sites

Ah, well, I've answered point 1.

$item = $this->wire('pages')->get('template=repeater_options, id=1255');

I'd love to know the answer to point 3 though. It would be extremely useful if that's an option. If I do a Page Reference field and set the selector string to template=repeater_options it would get every single item — whereas I'd only want the ones for the current page.

Link to comment
Share on other sites

1. If it's your code that's putting the repeater page IDs into session then you can count on them being what you expect and you don't necessarily have to validate them against a template or a repeater field. So you could just do...

$item = $pages->get($id)

...or...

$item = $pages($id);

 

2. It's because in the first case the selector is going to the database via PageFinder and in the second case the selector is searching within a PageArray in memory. There are a number of differences between what is possible with these two kinds of selectors but unfortunately those differences aren't described in the official documentation. Some related posts:
https://processwire.com/talk/topic/16067-find-repeater/?do=findComment&comment=148983
https://processwire.com/talk/topic/18343-selector-filter-for-multiple-dates/?do=findComment&comment=160451
https://processwire.com/talk/topic/20143-selector-is-case-sensitive/

 

3. For your Page Reference field you can use something like this as the "Selector string" for selectable pages:

id=page.your_repeater_field_name

Or you could use the Custom PHP option:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
	$page = $event->arguments('page');
	if($event->object->hasField == 'your_page_reference_field_name') {
		$event->return = $page->your_repeater_field_name;
	}
});

If you add new repeater items you'll need to save the page before you can select them, because PW doesn't know they exist until you do that.

  • Like 1
Link to comment
Share on other sites

Ah, thanks — id=page.your_repeater_field_name works perfectly!

Also, thanks for your explanation regarding point 2.

Regarding point 1, I worked it out, thanks, per my previous post. I am just storing an array of repeaterID => quantity in my session as I wanted to pull the live data on each request.

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