bookmark_bordergform_pre_render

Description

This filter is executed before the form is displayed and can be used to manipulate the prior to rendering the form.

This filter should be used in conjunction with the [gform_pre_validation], gform_pre_submission_filter, and gform_admin_pre_render filters to update the to be able to use those values elsewhere (merge tags in the confirmation and notification, for example).

Usage

The following would apply to all forms.

add_filter( 'gform_pre_render', 'your_function_name' );

To limit the scope of your function to a specific form, append the form id to the end of the hook name. (format: gform_pre_render_FORMID)

add_filter( 'gform_pre_render_6', 'your_function_name' );

Parameters

  • $form

    The current form to be filtered.

  • $ajax bool

    Is AJAX enabled.

  • $field_values array

    An array of dynamic population parameter keys with their corresponding values to be populated.

Examples

1. Populate Choices

This example dynamically populates a drop down, radio button or multi-select field with posts that are in the Business category.

add_filter( 'gform_pre_render', 'populate_choices' );

//Note: when changing choice values, we also need to use the gform_pre_validation so that the new values are available when validating the field.
add_filter( 'gform_pre_validation', 'populate_choices' );

//Note: when changing choice values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
add_filter( 'gform_admin_pre_render', 'populate_choices' );

//Note: this will allow for the labels to be used during the submission process in case values are enabled
add_filter( 'gform_pre_submission_filter', 'populate_choices' );
function populate_choices( $form ) {

    //only populating drop down for form id 5
    if ( $form['id'] != 5 ) {
       return $form;
    }

    //Reading posts for "Business" category;
    $posts = get_posts( 'category=' . get_cat_ID( 'Business' ) );

    //Creating item array.
    $items = array();

    //Adding initial blank value.
    $items[] = array( 'text' => '', 'value' => '' );

    //Adding post titles to the items array
    foreach ( $posts as $post ) {
        $items[] = array( 'value' => $post->post_title, 'text' => $post->post_title );
    }

    //Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
    foreach ( $form['fields'] as &$field ) {
        if ( $field->id == 8 ) {
            $field->choices = $items;
        }
    }

    return $form;
}

2. Populate Choices – Checkboxes

The following example dynamically populates a checkbox field with a list of published posts

//NOTE: update the '221' to the ID of your form
add_filter( 'gform_pre_render_221', 'populate_checkbox' );
add_filter( 'gform_pre_validation_221', 'populate_checkbox' );
add_filter( 'gform_pre_submission_filter_221', 'populate_checkbox' );
add_filter( 'gform_admin_pre_render_221', 'populate_checkbox' );
function populate_checkbox( $form ) {

    foreach( $form['fields'] as &$field )  {

        //NOTE: replace 3 with your checkbox field id
        $field_id = 3;
        if ( $field->id != $field_id ) {
            continue;
        }

        // you can add additional parameters here to alter the posts that are retreieved
        // more info: http://codex.wordpress.org/Template_Tags/get_posts
        $posts = get_posts( 'numberposts=-1&post_status=publish' );

        $input_id = 1;
        foreach( $posts as $post ) {

            //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
            if ( $input_id % 10 == 0 ) {
                $input_id++;
            }

            $choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
            $inputs[] = array( 'label' => $post->post_title, 'id' => "{$field_id}.{$input_id}" );

            $input_id++;
        }

        $field->choices = $choices;
        $field->inputs = $inputs;

    }

    return $form;
}

3. Populate Field With Values From Earlier Page

This example is for a two-page form. The data submitted from the first page is displayed on the second page as a preview. The second page has only one field, an html field that will be populated with the data from the first page.

add_filter( 'gform_pre_render_31', 'populate_html' );
function populate_html( $form ) {
    //this is a 2-page form with the data from page one being displayed in an html field on page 2
    $current_page = GFFormDisplay::get_current_page( $form['id'] );
    $html_content = "The information you have submitted is as follows:<br/><ul>";
    if ( $current_page == 2 ) {
        foreach ( $form['fields'] as &$field ) {
            //gather form data to save into html field (id 3 on my form), exclude page break
            if ( $field->id != 3 && $field->type != 'page' ) {
                //see if this is a complex field (will have inputs)
                if ( is_array( $field->inputs ) ) {
                    //this is a complex fieldset (name, adress, etc.) - get individual field info
                    foreach ( $field->inputs as $input ) {
                        //get name of individual field, replace period with underscore when pulling from post
                        $input_name = 'input_' . str_replace( '.', '_', $input['id'] );
                        $value = rgpost( $input_name );
                        $html_content .= '<li>' . $input['label'] . ': ' . $value . '</li>';
                    }
                } else {
                    //this can be changed to be a switch statement if you need to handle each field type differently
                    //get the filename of file uploaded or post image uploaded
                    if ( $field->type == 'fileupload' || $field->type == 'post_image' ) {
                        $input_name = 'input_' . $field->id;
                        //before final submission, the image is stored in a temporary directory
                        //if displaying image in the html, point the img tag to the temporary location
                        $temp_filename = RGFormsModel::get_temp_filename( $form['id'], $input_name );
                        $uploaded_name = $temp_filename['uploaded_filename'];
                        $temp_location = RGFormsModel::get_upload_url( $form['id'] ) . '/tmp/' . $temp_filename['temp_filename'];
                        if ( !empty( $uploaded_name ) ) {
                            $html_content .= '<li>' . $field->label . ': ' . $uploaded_name . "<img src='" . $temp_location . "' height='200' width='200'></img></li>";
                        }
                    } else {
                        //get the label and then get the posted data for the field (this works for simple fields only - not the field groups like name and address)
                        $html_content .= '<li>' . $field->label . ': ' . rgpost( 'input_' . $field->id ) . '</li>';
                    }
                }
            }
        }
        $html_content .= '</ul>';
        //loop back through form fields to get html field (id 3 on my form) that we are populating with the data gathered above
        foreach( $form['fields'] as &$field ) {
            //get html field
            if ( $field->id == 3 ) {
                //set the field content to the html
                $field->content = $html_content;
            }
        }
    }
    //return altered form so changes are displayed
    return $form;
}

4. Configure Conditional Logic

This example adds conditional logic to a form field (field 2) upon form load so that if the selection from field id 1 isnot Second Choice the field shows.

//assumes form id 221
add_filter( 'gform_pre_render_221', 'set_conditional_logic' );
add_filter( 'gform_pre_validation_221', 'set_conditional_logic' );
add_filter( 'gform_pre_submission_filter_221', 'set_conditional_logic' );
add_filter( 'gform_admin_pre_render_221', 'set_conditional_logic' );
function set_conditional_logic( $form ) {
    foreach ( $form['fields'] as &$field ) {
        if ( $field->id == 2 ) {
            $field->conditionalLogic =
                array(
                    'actionType' => 'show',
                    'logicType' => 'all',
                    'rules' =>
                        array( array( 'fieldId' => 1, 'operator' => 'isnot', 'value' => 'Second Choice' ) )
                );
        }
    }
    return $form;
}

Placement

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

Source Code

This filter is located in GFFormDisplay::get_form() in form_display.php