I'm using an ACF field from the options page to populate a Gravity Forms drop down. The values are pulling from the field but displaying at the top of the form (in preview), not in the drop down. I'm sure this is an issue in how I'm building the form. Thanks for help in advance!
See Screenshot
//gravity forms dynamically populate from ACF
add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', 'populate_posts' );
function populate_posts( $form ) {
global $choices;
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}
if( have_rows('core_values', 'option') ):
while( have_rows('core_values', 'option') ): the_row();
$choices[] = array( 'text' => the_sub_field('value_mstr_name'), 'value' => the_sub_field('value_mstr_name') );
endwhile;
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Post';
$field->choices = $choices;
endif;
}
return $form;
}
I hadn't defined choices as an array AND for ACF, the function the_sub_field is like an echo of the sub_field but get_sub_field actually propagates it into the GF field object choices property.
//gravity forms dynamically populate from ACF
add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', 'populate_posts' );
function populate_posts( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}
if( have_rows('core_values', 'option') ):
$choices = array();
while( have_rows('core_values', 'option') ): the_row();
$choices[] = array( 'text' => get_sub_field('value_mstr_name'), 'value' => get_sub_field('value_mstr_name') );
endwhile;
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Core Value';
$field->choices = $choices;
endif;
}
return $form;
}
You can also restrict this to a particular post type
add_filter( 'gform_pre_render_3', 'populate_warranty_form' );
add_filter( 'gform_pre_validation_3', 'populate_warranty_form' );
add_filter( 'gform_pre_submission_filter_3', 'populate_warranty_form' );
add_filter( 'gform_admin_pre_render_3', 'populate_warranty_form' );
function populate_warranty_form( $form ) {
foreach ( $form['fields'] as &$field ) {
// select field with css class 'product-name'
// this allows dynamic field to use existing css
if ( $field->type != 'select' || strpos( $field->cssClass, 'product-name' ) === false ) {
continue;
}
$args = array(
'post_type' => 'custom_post_type',
'posts_per_page'=> '-1',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC'
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
$postID = $post->ID;
if( have_rows('repeater_field', $postID) ): while( have_rows('repeater_field', $postID) ): the_row();
$choices[] = array( 'text' => get_sub_field('repeater_sub_field'), 'value' => get_sub_field('repeater_sub_field') );
endwhile; endif;
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select';
$field->choices = $choices;
}
return $form;
}