I managed to pass the values of a ACF checkbox field (colors) to a GF radio button field, showing the colors blue (first value) and red (second value). Does anyone know how to set the first value (e.g. blue) as default choice in the radio button field? My code is below. So how can I preselect the first value of the $radiobuttons
array?
add_filter( 'gform_pre_render_1', 'populate_radio_colors' );
add_filter( 'gform_pre_validation_1', 'populate_radio_colors' );
add_filter( 'gform_admin_pre_render_1', 'populate_radio_colors' );
add_filter( 'gform_pre_submission_filter_1', 'populate_radio_colors' );
function populate_radio_colors( $form ) {
if ( $form['id'] != 1 ) {
return $form;
}
$post_id = get_query_var('training')
$colors = get_field( 'colors' , $post_id );
// declare the array
$radiobuttons = array();
// set array
foreach ($colors as $color) {
$radiobuttons[] = array(
'value' => $color,
'text' => $color,
);
}
//Add repeater values as radio button choices
foreach ( $form['fields'] as &$field ) {
if ( $field->id == 49 ) {
$field->choices = $radiobuttons;
}
}
return $form;
wp_reset_postdata();
}