1

When I submit my gravity form entry, the $entry always returns my entry keys as the field ID's.

The problem with this is that I have multiple forms that perform similar functions using the gform_after_submission action.

Because gravity forms uses the field ID's as entry keys, it means I need separate functions or arrays to handle each form to do the same thing. Because my field ID's on each form are different.

Is there way change the gravity form entry field keys to a custom id string rather than integer and floats?

<pre>Array
(
    [id] => 63
    [status] => active
    [form_id] => 1
    [ip] => 172.25.0.1
    [source_url] => http://localhost/cart/
    [currency] => GBP
    [post_id] => 
    [date_created] => 2020-04-18 11:27:16
    [date_updated] => 2020-04-18 11:27:16
    [is_starred] => 0
    [is_read] => 0
    [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36
    [payment_status] => 
    [payment_date] => 
    [payment_amount] => 
    [payment_method] => 
    [transaction_id] => 
    [is_fulfilled] => 
    [created_by] => 1
    [transaction_type] => 
    [1.1] => 
    [3] => collection
    [24] => 
    [13] => Josh
    [14] => Moto
    [15] => test@gmail.com
    [16] => 01234567890
    [17] => 09876543210
    [18] => 09876543210
    [20.1] => pay-by-phone
    [21.1] => confirm-submission
    [22.1] => accept-terms
)
</pre>

Just an expansion of what i'm doing. See my code below.

add_action('gform_after_submission_1', [ $this, 'create_order' ], 10, 2 );

public function create_order( $entry, $form ) {

    // get the current cart data array
    $data = self::data();

    // create an order array
    $order = [
        'post_author'   => $user_id,
        'post_content'  => json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
        'post_type'     => 'purchase-order',
        'post_status'   => 'publish'
    ];

    // create order post using an array and return the post id
    $result = wp_insert_post($order);

    // if post id and is not a wp error then
    if($result && !is_wp_error($result)) {

        // get the id
        $post_id = $result;

        // update personal details
        update_field('order_first_name',$entry['13'],$post_id);

        // more update field functions...


    }

}

So what I'm trying to do as you can see with the update_field function.

Is that I'm using entry values by field id, for example $entry['13'].

But I would rather the field id's be strings, so i can use update_field like this...

update_field('order_first_name',$entry['order_first_name'],$post_id);

The returned $entry array would some how look like this...

Array
(
    [id] => 63
    [status] => active
    [form_id] => 1
    [ip] => 172.25.0.1
    [source_url] => http://localhost/cart/
    [currency] => GBP
    [post_id] => 
    [date_created] => 2020-04-18 11:27:16
    [date_updated] => 2020-04-18 11:27:16
    [is_starred] => 0
    [is_read] => 0
    [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36
    [payment_status] => 
    [payment_date] => 
    [payment_amount] => 
    [payment_method] => 
    [transaction_id] => 
    [is_fulfilled] => 
    [created_by] => 1
    [transaction_type] => 
    [order_install.1] => 
    [order_delivery_type] => collection
    [order_username] => 
    [order_first_name] => Josh
    [order_last_name] => Moto
    [order_email] => test@gmail.com
    [order_telephone] => 01234567890
    [order_telephone_2] => 09876543210
    [order_whatsapp] => 09876543210
    [order_terms_payment.1] => pay-by-phone
    [order_terms_confirm.1] => confirm-submission
    [order_terms_accept.1] => accept-terms
)

@DaveWiz Thanks for your answer, see how I used it.

/**
 * C
 * @param array $entry
 * @param array $form
 * @return void
 */
public static function key_conversion($entry, $form) {

    // admin value keys
    $order_first_name = 'order_first_name';

    // for each fields
    foreach( $form['fields'] as $field ) {

        // check case on admin label
        switch( $field->adminLabel ) {

            // order first name
            case $order_first_name:
                $entry[$order_first_name] = $entry[ $field->id ];
                unset($entry[ $field->id ]);
                break;

        }
    }

}

1 Answer 1

2

I'm assuming your underlying need is to assign a role for each field on different forms. So the Name field on Form A might be field ID 1 and the Name field on Form B might be field ID 2. Both fields represent the role of "Primary Contact Name". The simplest way to do this would be to give the field this "role" with the Admin Label field setting. Then you can fetch for the form object for the given entry, loop through the fields, check for your desired Admin Label (e.g. role) and do what you need to do with the data.

$form = GFAPI::get_form( $entry['form_id'] );

foreach( $form['fields'] as $field ) {
    switch( $field->adminLabel ) {
        case 'primary_contact_name':
            $primary_contact_name = $entry[ $field->id ];
            break;
    }
}

That's just a rough example of how it might work as a reliable way to get the specific type of data you want from multiple entries.

5
  • Hi Dave, I've elaborated on my answer and have left you a reply at the bottom my answer. Thanks
    – joshmoto
    Apr 19, 2020 at 16:37
  • Amazing, the penny has just dropped :-) See my extension of your answer in my answer. Thanks dude.
    – joshmoto
    Apr 19, 2020 at 18:26
  • So on hidden fields and html fields, admin label don't exist in the admin :-(
    – joshmoto
    Apr 20, 2020 at 11:02
  • Hidden fields are not displayed on the frontend so you could use their regular field label. HTML fields' label is not displayed so you could do the same for them; however, HTML fields do not have a "value" and are not saved in the entry. Apr 20, 2020 at 12:00
  • 1
    Yeah true, i switched to a normal text field and just used a css class to hide it on front end. HTML field you are right was not an issue. I have a single function now that i'm using on 3 forms to handle updating fields from the forms, cleaner and easier to read, less code.
    – joshmoto
    Apr 20, 2020 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.