Dynamic Dropdowns for Lists in Gravity Forms

I want to make one of the list fields in Gravity Forms a dynamic dropdown. I have the code to make one of the fields a dropdown and the code to make a dropdown dynamic. Please can someone help me combine the two snippets of code?

Here is the code for the dynamic dropdown

add_filter( 'gform_pre_render_17', populate_dropdown_operations_global_tag1 );
add_filter( 'gform_admin_pre_render_17', 
populate_dropdown_operations_global_tag1 );

function populate_dropdown_operations_global_tag1( $form ) {

global $wpdb;
global $loc_id;

$results = $wpdb->get_results( "SELECT name FROM custom_tag WHERE loc_id=$loc_id AND category='global_tag1' AND status='active' ORDER BY name ASC" );
$category = $wpdb->get_row( "SELECT name FROM custom_category WHERE loc_id=$loc_id AND category='global_tag1'" );

$choices = array();
$choices[] = array( "text" => "Select ".$category->name, "value" => " " );

foreach( $results as $result ) :
    $choices[] = array( "text" => $result->name, "value" => $result->name );
endforeach;

foreach( $form["fields"] as &$field ) :
    if( $field["id"] == '36' ) :
        $field["choices"] = $choices;
    endif;
endforeach;

return $form;
}

And here is the code for the dropdown in a list

add_filter( 'gform_column_input_17_36_1', 'set_column1', 10, 5 );
function set_column1( $input_info, $field, $column, $value, $form_id ) {
 return array( 'type' => 'select', 'choices' => 'NEED TO ADD CHOICES HERE' );
}

Thanks!

Read more here: Dynamic Dropdowns for Lists in Gravity Forms

Leave a Reply

Your email address will not be published. Required fields are marked *