Search code examples
gravity-forms-pluginchainauto-populategravityformschained-select

Gravity Form-Dynamic Pop. the Chained Select



Bit of a newb here. Teaching myself PHP/java/etc. as a go. I've been using THIS using GF's Document page to create a Chained Select that will pull a Company Name(from a previous GF entry) as Parent, but I am having trouble with the Child's (which is the company's Features).

I was pretty proud of myself when I made a got the Parent of my Chained Select to fire using the following code:

add_filter("gform_pre_render_14", "populate_companies");
add_filter("gform_admin_pre_render_14", "populate_companies");
add_filter( "gform_chained_selects_input_choices_14_5_1", "populate_companies");
 function populate_companies( $form, $input_items, $form_id, $field, $input_id, $chain_value ){
  if( $form["id"] != 14 )
  
         return $form;
   
  $items = array();
   
$form_id = '7';
$entries = GFAPI::get_entries( $form_id );

   if (is_array($entries))
{
	foreach($entries as $fcompany) $items[] = array( 'value' => rgar( $fcompany, '4' ), 'text' => rgar( $fcompany, '4' ), 'isSelected' => false );
}
     foreach($form["fields"] as &$field)
        if($field["id"] == 5){
            $field["choices"] = $items;
        }
    return $form;
}

Now, that code works. Maybe it's not pretty, but it works. So the next step would be to get it to populate the Child field. I've been able to get this far, but am stumped...

add_filter("gform_pre_render", "populate_features");
add_filter("gform_admin_pre_render", "populate_features");
add_filter( "gform_chained_selects_input_choices_14_5_2", "populate_features");
 function populate_features( $form, $input_items, $form_id, $field, $input_id, $chain_value ){
  if( $form["id"] != 14 )
  
         return $form;
   
  $items = array();
  $selected_fcompany = $chain_value[ "{$field->id}.1" ];
   if( ! $selected_fcompany ) {
        return $input_choices;
    }
   
$form_id = '7';
$entries = GFAPI::get_entries( $form_id );

   if (is_array($entries))
{
	foreach($entries as $cfeature) $items[] = array( 'value' => rgar( $cfeature, '10' ), 'text' => rgar( $cfeature, '10' ), 'isSelected' => false );
}
     foreach($form["fields"] as &$field)
        if($field["id"] == 5){
            $field["choices"] = $items;
        }
    return $items;
}

I feel the problem is with the chained_value. Heck if I can get it to work through the first foreach in the first snippet that I'll do the dance of joy. Any ideas out there?


Solution

  • Ended up going with the Gravity Plus plugin. It's been really handy, but can't have more than one on a form, which is kinda lame. It take some work to get it to work, but let me know of you need help!

    https://gravityplus.pro/gravity-forms-dynamic-population/