bookmark_bordergform_chained_selects_input_choices

Description

Use this filter 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_action( 'gform_chained_selects_input_choices', 'your_function_name' );

Filter all chained selects for a specific form.

add_action( 'gform_chained_selects_input_choices_6', 'your_function_name' );

Filter chained selects for a specific form and specific field.

add_action( 'gform_chained_selects_input_choices_6_2', 'your_function_name' );

Filter a specific chained select for a specific form and field.

add_action( '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
    );

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, 5 );
function gf_populate_makes( $input_choices, $form_id, $field, $input_id, $chain_value ) {

	$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, 5 );
function gf_populate_models( $input_choices, $form_id, $field, $input_id, $chain_value ) {

	$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, 5 );
function gf_populate_years( $input_choices, $form_id, $field, $input_id, $chain_value ) {

	$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;
}

Placement

This code should be placed in the functions.php file of your active theme.

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 );

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.