Support » Plugin: Drop Down Options in List Fields for Gravity Forms » Dynamic population not working

  • Resolved MaryMak

    (@maxms)


    Hello,

    This plugin is great — thank you!

    I have having issues with the dynamic population. I eventually need the drop down in one column to populate with titles from a custom post type. I started with just the base filter code you provided and changed the form id, field id and column label to match what I’ve got. Nothing happened. Has anything changed that you know of that might have made this code no longer functional? My test code is below.

    Wondering if you could also give advice on how to adapt this to pull in custom post types? Something along these lines: automatically populate a dropdown with post titles?

    Thank you for your time!

    add_filter( 'gform_pre_render', 'my_gform_pre_render' );
    add_filter( 'gform_pre_validation', 'my_gform_pre_render' );
    add_filter( 'gform_admin_pre_render', 'my_gform_pre_render' );
    add_filter( 'gform_pre_submission_filter', 'my_gform_pre_render' );
    
    function my_gform_pre_render( $form ) {
        if ( GFCommon::is_form_editor() || 4 != $form['id'] ) {
            return $form;
        }
    
        if ( is_array( $form ) || is_object( $form ) ) {
    
            foreach ( $form['fields'] as &$field ) {  // for all form fields
                $field_id = $field['id'];
    
                if ( 15 != $field_id ) {
                    break;
                }
    
                if ( 'list' == $field->get_input_type() ) {
    
                    $has_columns = is_array( $field->choices );
    
                    if ( $has_columns ) {
                        foreach ( $field->choices as $key => &$choice ) { // for each column
                            $isDropDown = rgar( $choice, 'isDropDown' );
                            $column = rgars( $field->choices, "{$key}/text" );
                            if ( $isDropDown && 'Select a Nonprofit' == $column ) {
                                $choices = 'Option 1, Option 2, Option 3';
                                $choice['isDropDownChoices'] = $choices;
                            }
                        }
                    }
                }
            }
        }
        return $form;
    }

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • So… I got this to work! When I removed the field ID code, the base code you provided worked. Then I added code to pull in the posts. I also specified the form ID in the filter hooks instead. My final snippet is below.

    /*******************************************************
     * Populate Donation Form dropdown with Nonprofit Posts
     * ****************************************************/
    
    add_filter( 'gform_pre_render_4', 'my_gform_pre_render' );
    add_filter( 'gform_pre_validation_4', 'my_gform_pre_render' );
    add_filter( 'gform_admin_pre_render_4', 'my_gform_pre_render' );
    add_filter( 'gform_pre_submission_filter_4', 'my_gform_pre_render' );
    
    function my_gform_pre_render( $form ) {
        if ( is_array( $form ) || is_object( $form ) ) {
    
            foreach ( $form['fields'] as &$field ) {  // for all form fields
                if ( 'list' == $field->get_input_type() ) {
    
                    $has_columns = is_array( $field->choices );
    
                    if ( $has_columns ) {
                        foreach ( $field->choices as $key => &$choice ) { // for each column
                            $isDropDown = rgar( $choice, 'isDropDown' );
                            $column = rgars( $field->choices, "{$key}/text" );
    						$posts = get_posts( 'post_type=nonprofit&numberposts=-1&post_status=publish&order=ASC' );
                            if ( $isDropDown && 'Select a Nonprofit' == $column ) {
    							$choices = array();
    							echo implode( ', ', $choices );
    							$choices[] = array("text" => "Select an Organization", "value" => "");
    							foreach ( $posts as $post ) {
    								$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
    							}
    							$choice['isDropDownChoices'] = $choices;
                            }
                        }
                    }
                }
            }
        }
        return $form;
    }

    Update: This worked to initially populate the dropdown but it doesn’t add any new posts added so I am looking for a solution to that.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Dynamic population not working’ is closed to new replies.