Jump to content

Search field in the first repeater


diegonella
 Share

Recommended Posts

I'm making a site for a travel agency, excursions in the section, which has a repeater field cities in which loaded the itinerary.
 
Then in the search frontend need search all trips leaving from "berlin"
 
Trips
   Trip 1
      Cities (repeater)
         Berlin
         Rome
         Paris
 
   Trip 2
      Cities (repeater)
         Paris
         Rome
 
 
I need to find all the children in the repeater field equals berlin cities, but only the first record
How do the selector?
 
$p = $pages->get("/trips/")->children("cities_repeater.0.city=berlin");

??

Link to comment
Share on other sites

I'm afraid there's no selector you could use to filter based on nth record in a repeater. But there are other ways to get there. I'm assuming you've got a template "trip" with a repeater field "cities_repeater", correct? I'm asking because you're talking about a repeater but the structure (and get()->children()) looks like you've got pages inside each other.

So, one option is to find all trips via Berlin, loop through the results and pick only those that have Berlin as a starting location. If the amount of trips starting from a city isn't going to grow too much (so that you don't need to paginate the results) and the amount of trips via a city doesn't get too high either, this would probably work out just fine.

On the other hand, you can make the trips searchable with a selector based on their starting city with a little autoload module and a hook. Create a new field "starting_city", add it to the trip template and make it hidden. Then create a module hooking after Pages::save. In the hook check for the trip template and then populate starting_city with the first element of cities_repeater. And remember to save only the starting_city field to avoid an infinite loop :). See Pete's example and Soma's comments in this thread for a code example.

Now that you've got starting_city in a separate field it's a walk in the park to write the selector you're after:

$trips = $pages->find("parent=/trips/, starting_city=berlin");
Do remember that in order to get that hidden field populated for all the trips, the hooking module must be installed before any trips are actually created. If you've already got the data in place, then you need to get the module working and either save each trip (no changes needed, just edit+save and the hook will kick in) or create a little script that populates the field for the first time.
  • Like 3
Link to comment
Share on other sites

Thank you very much Nik
Yes, at the end I did with a hook
 
 
public function init() {
	$this->pages->addHookBefore('save', $this, 'hookSave');
}

public function hookSave($event) {
	$page = $event->arguments[0];

	# Guardar la ciudad de salida y arribo
	if( $page->template == 'salida_child' && $page->isChanged('itinerario_destino_repeater') ) {
		if ( count($page->itinerario_destino_repeater) ){
			$salida_origen = $salida_destino = "";
			foreach ($page->itinerario_destino_repeater AS $itinerario ) {
				if ( $itinerario->itinerario_ciudad->id ) {
					if ( $salida_origen == "" ) { $salida_origen = $itinerario->itinerario_ciudad; }
					$salida_destino = $itinerario->itinerario_ciudad;
				}
			}
			$this->message("Itinerario desde <b>".$salida_origen->title."</b> hasta <b>".$salida_destino->title."</b>");
			$page->salida_origen = $salida_origen;
			$page->salida_destino = $salida_destino;
		}
	}
}
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...