Description
The gravityflowformconnector_[type] filters allows the entry to be modified before create, update, submission.
Usage
The actual filter name to hook on depends on which token type you are wanting to filter.
add_filter( 'gravityflowformconnector_new_entry', 'sh_gravityflowconnector_adjust_new_entry', 10, 5);
add_filter( 'gravityflowformconnector_update_entry', 'sh_gravityflowformconnector_update_entry', 10, 5);
add_filter( 'gravityflowformconnector_form_submission', 'sh_gravityflowformconnector_form_submission', 10, 5);
add_filter( 'gravityflowformconnector_update_field_values', 'sh_gravityflowformconnector_update_field_values', 10, 5);
Parameters
Parameter | Type | Details |
---|---|---|
$new_entry | Entry | The new entry |
$entry | Entry | The current entry |
$form | Form | The current form |
$target_form | Form | The target form |
$step | Step | The current workflow step |
Examples
gravityflowformconnector_new_entry
This filter provides a way to modify the new entry before it is created in separate form / site.
Include 10% into the cost of a numeric field
add_filter( 'gravityflowformconnector_new_entry', 'sh_gravityflowconnector_adjust_new_entry', 10, 5);
function sh_gravityflowconnector_adjust_new_entry( $new_entry, $entry, $form, $target_form, $step ) {
//Adding 10% to the total cost
$new_entry['3'] = $new_entry['3'] * 1.10;
return $new_entry;
}
Ensure a field with ‘No Duplicates’ specified in settings is respected when attempting to create a new entry.
The New Entry step relies on the Gravity Forms API for add_entry which docs state that, “The usual hooks that are triggered while saving entries are not fired here.” You would need to adjust the step ID and form/field IDs to match your scenario. The add_note will log a message into the timeline of the entry executing the FC New Entry to indicate the entry was not created as expected.
add_filter( 'gravityflowformconnector_new_entry', 'sh_gravityflowconnector_adjust_new_entry', 10, 5 );
function sh_gravityflowconnector_adjust_new_entry( $new_entry, $entry, $form, $target_form, $step ) {
//Change Step ID (121) to match your case and update the field_filters to match your target form field ID / entry field IDs
if ( 121 == $step->get_id() ) {
$search_criteria = array(
'status' => 'active',
'field_filters' => array(
'mode' => 'all',
array(
'key' => '2',
'value' => $entry['2'],
),
),
);
if ( GFAPI::count_entries( $target_form['id'], $search_criteria ) > 0 ) {
//An existing entry exists in the target form so new entry should not be created. Pass a blank array back from filter
$step->add_note( "No entry created - duplicate value exists in Form " . $target_form['id'] . " Field 2.");
$new_entry = array();
}
}
return $new_entry;
}
gravityflowformconnector_update_field_values
Use the Update Fields step type and copy only the last comment from a Discussion Field into the selected entry
add_filter( 'gravityflowformconnector_update_field_values', 'sh_gravityflowconnector_copy_discussion', 10, 5 );
function sh_gravityflowconnector_copy_discussion( $new_entry, $entry, $form, $target_form, $step ) {
//Change Step ID (125) to match your workflow and update the field id (4) source / target field IDs
if ( 125 == $step->get_id() ) {
$discussions = json_decode( $entry['4'], ARRAY_A );
if ( is_array( $discussions ) ) {
//Select the specific discussion comment(s) you want and re-encode the array into JSON notation
$last_comment = array( end( $discussions ) );
$new_entry['4'] = json_encode( $last_comment );
}
}
return $new_entry;
}
gravityflowformconnector_update_entry
Use the Update an Entry step to switch a user field value in a workflow with a User Registration feed
This scenario would apply if your form has a User Registration feed setup to create a new user and you want to assign tasks after the registration step in the workflow to the “not yet created” user. In order to make use of it you would:
- Add a User field to the form with visibility set to Administrative.
- Add a Form Connector Update an Entry step in between the User Registration step and the step(s) which need the new assignee.
- Setup the field mappings to attempt to put the email field into the user field
- Select the User field as the assignee for the steps that require the ‘to be created’ user to action.
Then include this snippet to change the mapping to perform a lookup of the new user by the email field.
add_filter( 'gravityflowformconnector_update_entry', 'jo_gravityflowconnector_update_created_user', 10, 5 );
function jo_gravityflowconnector_update_created_user( $update_entry, $entry, $form, $target_form, $step ) {
// Change to match the Step ID you want the swap to occur on.
if ( $step->get_id() != '175' ) {
return $update_entry;
}
$potential_user = get_user_by( 'email', $entry['2'] );
if ( ! $potential_user ) {
// Replace with user ID that should receive the task if the user registration were to fail for other reasons.
$update_entry['3'] = '';
} else {
$update_entry['3'] = $potential_user->ID;
}
return $update_entry;
}
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?