Description
The gform_chained_selects_input_choices filter is used to modify the choices that will appear in each drop down of a Chained Select field.
Usage
Filter all chained selects for all forms.
add_filter( 'gform_chained_selects_input_choices', 'your_function_name' );
Filter all chained selects for a specific form.
add_filter( 'gform_chained_selects_input_choices_6', 'your_function_name' );
Filter chained selects for a specific form and specific field.
add_filter( 'gform_chained_selects_input_choices_6_2', 'your_function_name' );
Filter a specific chained select for a specific form and field.
add_filter( 'gform_chained_selects_input_choices_6_2_1', 'your_function_name' );
Parameters
- $input_choices array
An array of Choices that will be used to populate the current drop down. - $form_id integer
The form ID for the current form. - $field Field Object
The current field object. - $input_id float
The input ID of the current input (i.e. drop down) being processed. Example: 3.1. - $full_chain_value array
An array of values representing the selected value for each input in the chain of selects. Example:array( '3.1' => 'Toyota', '3.2' => 'Matrix', '3.3' => 2013 );
- $value string
The value of the current chained select. - $index int – Added in version 1.1.
The index of the specific chained select.
Examples
1. Populate Year, Make, and Model
// populates the "Make" drop down of our Chained Select
add_filter( 'gform_chained_selects_input_choices_855_14_1', 'gf_populate_makes', 10, 7 );
function gf_populate_makes( $input_choices, $form_id, $field, $input_id, $chain_value, $value, $index ) {
$api_key = '9du7r286jng2zjjaf8antxur'; // signup for your own Edmunds API key here: http://edmunds.mashery.com/member/register
$choices = array();
$response = wp_remote_get( "https://api.edmunds.com/api/vehicle/v2/makes?state=new&year=" . date( 'Y' ) . "&view=basic&fmt=json&api_key={$api_key}" );
if ( wp_remote_retrieve_response_code( $response ) != 200 ) {
return $input_choices;
}
$makes = json_decode( wp_remote_retrieve_body( $response ) )->makes;
foreach( $makes as $make ) {
$choices[] = array(
'text' => $make->name,
'value' => $make->niceName,
'isSelected' => false
);
}
return $choices;
}
// populates the "Model" drop down of our Chained Select
add_filter( 'gform_chained_selects_input_choices_855_14_2', 'gf_populate_models', 10, 7 );
function gf_populate_models( $input_choices, $form_id, $field, $input_id, $chain_value, $value, $index ) {
$api_key = '9du7r286jng2zjjaf8antxur'; // signup for your own Edmunds API key here: http://edmunds.mashery.com/member/register
$choices = array();
$selected_make = $chain_value[ "{$field->id}.1" ];
if( ! $selected_make ) {
return $input_choices;
}
$response = wp_remote_get( "https://api.edmunds.com/api/vehicle/v2/{$selected_make}/models?view=basic&fmt=json&api_key={$api_key}" );
if ( wp_remote_retrieve_response_code( $response ) != 200 ) {
return $input_choices;
}
$models = json_decode( wp_remote_retrieve_body( $response ) )->models;
foreach( $models as $model ) {
$choices[] = array(
'text' => $model->name,
'value' => $model->niceName,
'isSelected' => false
);
}
return $choices;
}
// populates the "Year" drop down of our Chained Select
add_filter( 'gform_chained_selects_input_choices_855_14_3', 'gf_populate_years', 10, 7 );
function gf_populate_years( $input_choices, $form_id, $field, $input_id, $chain_value, $value, $index ) {
$api_key = '9du7r286jng2zjjaf8antxur'; // signup for your own Edmunds API key here: http://edmunds.mashery.com/member/register
$choices = array();
$selected_make = $chain_value[ "{$field->id}.1" ];
$selected_model = $chain_value[ "{$field->id}.2" ];
if( ! $selected_make || ! $selected_model ) {
return $input_choices;
}
$response = wp_remote_get( "https://api.edmunds.com/api/vehicle/v2/{$selected_make}/{$selected_model}/years/count?view=full&fmt=json&api_key={$api_key}" );
if ( wp_remote_retrieve_response_code( $response ) != 200 ) {
return $input_choices;
}
$years = wp_list_pluck( json_decode( wp_remote_retrieve_body( $response ) )->years, 'year' );
rsort( $years );
foreach( $years as $year ) {
$choices[] = array(
'text' => $year,
'value' => $year,
'isSelected' => false
);
}
return $choices;
}
2. Define the default choice
The following example shows how you can define which choice in the drop down should be selected by default. In this case we are setting the second choice as the default choice in the third drop down of field 1 on form 123.
add_filter( 'gform_chained_selects_input_choices_123_1_3', function( $choices ) {
$choices[1]['isSelected'] = true;
return $choices;
} );
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Source Code
$input_choices = gf_apply_filters( 'gform_chained_selects_input_choices', array( $this->formId, $this->id, $index ), $input_choices, $this->formId, $this, $input_id, $full_chain_value, $value, $index );
This hook is located in GF_Chained_Field_Select::get_input_choices() in class-gf-field-chained-select.php.
Since
- The form specific version of this hook was added in Gravity Forms Chained Selects 1.0.
- The $index parameter was added in version 1.1.