Search code examples
wordpressgravity-forms-plugingravityforms

WordPress - Gravity Forms - Create field dynamically


Trying to create new fields for my form dynamically, as I'm getting json from 3rd party API. Based on this json, I need a number of fields added to my form - not fixed number. So, I'm doing this, hooking it onto gform_pre_render:

add_filter( 'gform_pre_process', 'create_products_gforms' );
add_filter( 'gform_admin_pre_render', 'create_products_gforms' );
add_filter( 'gform_pre_render', 'create_products_gforms' );
function create_products_gforms( $form ) {

    $helper = new NSHelper();
    $state_name = $_POST['input_7'] ?? '';

    $code_value = $helper->get_state_code_by_name( $state_name );

    // Only fetch products if current form id is 33, state code is defined
    // and if there are products for this state.

    if ( $form['id'] != 33 || !$code_value ) {
       return $form;
    }

    // Get product list from NetSuit API based on state code
    $products_json_data = get_products_data( $code_value );

    $products = json_decode( json_decode( $products_json_data ) );

    $new_field_id = 0;
    foreach( $form['fields'] as $field ) {
        if( $field->id > $new_field_id ) {
            $new_field_id = $field->id;
        }
    }

    $new_field_id++;

    foreach ( $products as $product_object ) {
        // Prepare field properties
        $props = array(
            'id'        => $new_field_id,
            'type'      => 'singleproduct',
            'label'     => $product_object->onlinedisplayname,
            'basePrice' => floatval( $product_object->price ),
            'enableCalculation' => true
        );

        // Create new gravity forms field and add it to the form object
        $nf = GF_Fields::create( $props );

        // Hack - insert into array at specific position
        // Needed to display product fields before other fields
        // in the form
        array_splice( $form['fields'], 11, 0, array($nf) );

        $new_field_id++;
    }

    GFAPI::update_form( $form );

    $form['dynamic_fields_ids'] = $added_fields_ids;

    return $form;
}

This works, ie, it shows fields properly on the frontend. Now, the issue with this is that, once form is submitted, all the fields except these dynamically added ones are in the submission. But these are not. I assumed this has to do that these fields aren't registered in the form, so I also tried GFAPI::update_form( $form );, but that didn't help with submission part, though it udpates my form with new fields in the backend too.

Any ideas?


Solution

  • Based on your use-case, Milos, I would suggest using the gform_form_post_get_meta filter:

    https://docs.gravityforms.com/gform_form_post_get_meta/

    This will fire every time the form is fetched from the database and the most sure-fire way of guaranteeing your fields will be present.

    If you prefer to be more surgical and stick with the gform_pre_render approach, you'll want to apply the same function on a couple other filters:

    gform_pre_process
    gform_admin_pre_render