Gravity form gform_after_submission is not working - gravity-forms-plugin

I am trying to submit the form data to a third party on successful submission of gravity form. But I am stuck at the first stage. Looks like the function is not running or may be something wrong with my code. Here is my code:
add_action( 'gform_after_submission_8', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
$body = array(
'name' => rgar( $entry, '1' ),
'email' => rgar( $entry, '2' ),
'phone' => rgar( $entry, '3' ),
'case_details' => rgar( $entry, '4' ),
);
print_r($body);
}
Any help will be appreciated. Thanks in advance.

Related

How can I save dynamically created Gravity Form fields into entries content?

The code below successfully creates a dynamic field and for a Gravity Form, and on submission the dynamic field data is included in the email content sent. However the dynamic field isn't saved in the "entries" data for that form submission, only the fields created manually using the plugin are. Anyone know how to include this data in the entries data saved?
add_filter('gform_pre_render_5', 'populate_wines');
add_filter('gform_pre_validation_5', 'populate_wines');
function populate_wines($form) {
// create dynamic select field
$props = array(
'id' => 51,
'type' => 'select',
'label' => 'Dynamic field label',
'choices' => array(
array(
'text' => '',
'value' => '',
),
array(
'text' => '1',
'value' => '1',
),
array(
'text' => '2',
'value' => '2',
),
array(
'text' => '3',
'value' => '3',
),
)
);
$new_field = GF_Fields::create( $props );
$form['fields'][] = $new_field;
return $form;
}
Edit: Dave's solution below works great for the above function (thanks!!), but adding to the function to include the looped wine list (code below), it doesn't work, any ideas?
function populate_wines($form) {
// options for select lists
$select_choices = array(
array(
'text' => '',
'value' => '',
),
array(
'text' => '1',
'value' => '1',
),
array(
'text' => '2',
'value' => '2',
),
array(
'text' => '3',
'value' => '3',
),
);
// loop through wine list
if( have_rows('wine_options') ):
$wine_count = 50;
while( have_rows('wine_options') ) : the_row();
$wine_ID = get_sub_field('wine_option');
$wine_name = get_the_title($wine_ID);
// create wine select field
$props = array(
'id' => $wine_count,
'type' => 'select',
'label' => $wine_name,
'choices' => $select_choices
);
$new_field = GF_Fields::create( $props );
$form['fields'][] = $new_field;
$wine_count++;
endwhile;
endif;
if ( GFForms::get_page() !== 'form_editor' ) {
return $form;
}
}
You'll need to call the same function on the gform_admin_pre_render filter. My recommendation would be to also add a check to exclude it from being output in the form editor but you may want that. If you don't, it'd look something like this:
if ( GFForms::get_page() !== 'form_editor' ) {
return $form;
}

Combined ANY/ALL (AND/OR) in GravityForms GFAPI::get_entries?

Can GFAPI::get_entries take a NESTED set of field_filters, so the top filter uses 'mode' => 'all' and the child filter uses 'mode' => 'any' ?
My goal is to find entries where
Color = 'Red' AND City = 'Chicago' OR 'Boston'
The docs for GFAPI do not mention this capability, but there are not many examples anyway. I tried the following code, but it results in a 500-error.
$form_id = '11';
$search_criteria = array(
'field_filters' => array(
'mode' => 'all' // 'and'; default
array(
'key' => '23', // color
'value' => 'red'
),
array(
'mode' => 'any', // 'or'
array(
'key' => '12', // city
'value' => 'Chicago'
),
array(
'key' => '12',
'value' => 'Boston'
),
),
) // field_filters
); // $search_criteria
$entries = GFAPI::get_entries( $form_id, $search_criteria );
Alternatively, I can do the 2nd filter (for City) separately, in a foreach ($entries as $entry), if necessary.
Wondering if anyone has tried something like this and could advise.
Thanks.
This isn't possible with the GFAPI::get_entries method; however, if you're willing to go beneath the hood a little further, you can use the GF_Query class to do complex clauses as you've described.
Take a look at this simple form export. I've setup two Drop Down fields, each with three choices. I've submitted three sample entries:
Now, let's say we only want to get entries where Drop Down A is "First Choice" and Drop Down B is "Second Choice" or "Third Choice". That'd look something like this:
$form_id = 387;
$gf_query = new GF_Query( $form_id );
$gf_query->where( call_user_func_array( array( 'GF_Query_Condition', '_and' ), array(
new GF_Query_Condition(
new GF_Query_Column( '1' ),
GF_Query_Condition::EQ,
new GF_Query_Literal( 'First Choice' )
),
call_user_func_array( array( 'GF_Query_Condition', '_or' ), array(
new GF_Query_Condition(
new GF_Query_Column( '2' ),
GF_Query_Condition::EQ,
new GF_Query_Literal( 'Second Choice' )
),
new GF_Query_Condition(
new GF_Query_Column( '2' ),
GF_Query_Condition::EQ,
new GF_Query_Literal( 'Third Choice' )
),
) ),
) ) );
$entries = $gf_query->get();
This is the same basic structure as your example with the color and city. Just update the form ID, field IDs, and field values and you'll be good to go.

Integrating Gravity Forms with a 3rd Party (Eloqua)

I wondering if you guys could help. I am trying to integrate Gravity forms with Eloqua CRM. I've tried both '3rd Party Integration' plugin and I have also tried adding the gf after submission function in my functions.php file. However, I have not been able to get this to work. My code is below. I entered the url of my form in 'eloqua' (https://s1913652004.t.eloqua.com/e/f2) There is also a form id, but I don't see anywhere to put that.
add_action( 'gform_after_submission_1', 'post_to_third_party', 10, 2 ); function post_to_third_party( $entry, $form ) {
$post_url = 'https://s1913652004.t.eloqua.com/e/f2';
$body = array(
'first_name' => rgar( $entry, '1.3' ),
'last_name' => rgar( $entry, '1.6' ),
'email' => rgar( $entry, '2' ),
'country' => rgar( $entry, '3' ),
);
GFCommon::log_debug( 'gform_after_submission_1: body => ' . print_r( $body, true ) );
$request = new WP_Http();`enter code here`
$response = $request->post( $post_url, array( 'body' => $body ) );
GFCommon::log_debug( 'gform_after_submission_1: response => ' . print_r( $response, true ) );
Use the Gravity Forms Eloqua Add-On. It's actively maintained.

How to receive REMOTE POST response data and print to Gravity Forms confirmation page

I'm struggling with getting remote post response data to print on the Gravity Forms confirmation page. The data from the form is posted to the 3rd party site successfully and my logs show that the response data is received, however the received data is not printing to the confirmation page.
I have tried many variations such as;
$request = new WP_Http();
$data = json_decode( wp_remote_retrieve_body( $response ) );
$response = wp_remote_post( $post_url, array( 'body' => $body ) );
GFCommon::log_debug( 'gform_confirmation: response => ' . print_r( $response, true ) );
return $confirmation;
I submit the data via a plugin and have tried with the filters gform_confirmation, gform_after_submission, and gform_entry_post_save to no avail.
After many support tickets to Gravity Forms; I'm told that to do this requires additional scripting.
Thank you,
Richard
This is the code I have so far for the plugin.
add_filter( 'gform_confirmation_2', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
$post_url = 'my_post_url';
$body = array(
'VTC_ID' => rgar( $entry, '15' ),
'Member_ID' => rgar( $entry, '16' ),
'bname' => rgar( $entry, '3' ),
'baddress' => rgar( $entry, '4' ),
'bcity' => rgar( $entry, '5' ),
'bstate' => rgar( $entry, '6' ),
'bcountry' => rgar( $entry, '7' ),
'bzip' => rgar( $entry, '8' ),
'phone' => rgar( $entry, '9' ),
'email' => rgar( $entry, '17' ),
'password' => rgar( $entry, '11' ),
'isTrial' => rgar( $entry, '12' ),
'isActive' => rgar( $entry, '18' ),
'trialStart' => rgar( $entry, '13' ),
'trialEnd' => rgar( $entry, '14' ),
);
GFCommon::log_debug( 'gform_confirmation: body => ' . print_r( $body, true ) );
$request = new WP_Http();
$response = wp_remote_post( $post_url, $parameters );
$confirmation .= print_r( $response, true );
return $confirmation;
GFCommon::log_debug( 'gform_confirmation: response => ' . print_r( $response, true ) );
}
Assuming everything is part of the same submission process, you should be able to use the gform_confirmation hook instead of the gform_after_submission.
add_filter( 'gform_confirmation_123', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
$response = wp_remote_post( $post_url, $parameters );
$confirmation .= print_r( $response, true );
return $confirmation;
}
This assumes that:
Your confirmation is configured for text (not page or redirect)
You will need to:
Update the "123" in the filter name to your form ID
Update the wp_remote_post() to actually use the variables applicable to your request
Update the $confirmation to include the actual content from the response that you want
Starting with #dave-from-gravity-wiz answer, I used GET not POST with github API for simplicity when experimenting
add_filter( 'gform_confirmation_4', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
$response = wp_remote_get( 'https://api.github.com/users/someuser' );
if ( is_wp_error( $response ) ) {
echo 'An error happened';
} else {
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );
}
$confirmation .= print_r( $data->id, true );
return $confirmation;
}
This takes the json table you can see on the url and only prints the 'id' number in the response on the form submission confirmation, or any key label you change it to. If you want to show more than one value I was told you can concatenate the print_r so this works in the above example:
$confirmation .= print_r( $data->id, true )." <br/> ".print_r($data->login, true);
return $confirmation;

sending form values $form_state['redirect'] Form API Drupal

I want to send my form values using form API drupal. I have following value
$form['billing']["cardholders_name"] = array(
'#type' => 'textfield',
'#title' => t("Cardholder's Name"),
'#required' => TRUE,
'#prefix' => '<div class="field-wrapper-w1 card-name">'
);
I am writing following code in my form submit function
function test_form_submit($form, &$form_state) {
$form_state['redirect'] = 'www.test.com/page' . '?cname=' . $form_state['values']['billing[cardholders_name]'];
}
But it seems like it is not working. Please help
The following example appears in
http://api.drupal.org/api/drupal/includes%21form.inc/function/drupal_redirect_form/7
$form_state['redirect'] = array(
'node/123',
array(
'query' => array(
'foo' => 'bar',
),
'fragment' => 'baz',
),
);
Any value for form element will be in $form_state under values so in your function test_form_submit you can access cardholders_name
$form_state['values']['cardholders_name']
also you can do it in this way using drupal_goto()
drupal_goto('www.test.com/page' . '?cname=' . $form_state['values']['cardholders_name]);