gravityflow_step_complete

The gravityflow_step_complete action fires on the completion of a step.

Examples

Example 1 -  At the conclusion of a step write the value from a text field into the timeline and blank out the field value.

add_action( 'gravityflow_step_complete', 'sh_gravityflow_step_complete', 10, 4 );
function sh_gravityflow_step_complete( $step_id, $entry_id, $form_id, $status ) {
    if ( $step_id == '5' ) {
        $entry = GFAPI::get_entry( $entry_id );
        if ( ! is_wp_error( $entry ) && isset( $entry['1'] ) && ! empty( $entry['1'] ) ) {
            gravity_flow()->add_timeline_note( $entry_id, 'Message from customer: ' . $entry['1'] );
            GFAPI::update_entry_field( $entry_id, '1', '' );
        }
    }
}

Example 2 - Updating User Field from Email Field

Update a hidden user field on a form based on a visible email field. This might be invoked on the conclusion of an Approval Step

add_action( 'gravityflow_step_complete', 'sh_gravityflow_step_complete', 10, 4 );
function sh_gravityflow_step_complete( $step_id, $entry_id, $form_id, $status ) {
	//Field 2 = Email
	//Field 3 = User Field
	// ensure correct step id
	if ( $step_id == '192' ) {
        	// get the entry details
        	$entry = GFAPI::get_entry( $entry_id );
        	// get user by email input
        	$user = get_user_by( 'email', $entry[2] );
        	// update the user ID in the hidden user field if it matches
		if ( ! $user ) {
	        	GFAPI::update_entry_field( $entry_id, '3', $user->ID );
		}
}
}

Example 3 - Restarting a step in Form A based on the completion of a step in Form B

This example might be useful in multiple scenarios:

  • You have used the Form Connector Update an Entry step with a field mapping to change the "Workflow Step" of a different entry.
  • You want to have an Approval action in Form A also cause step change/restart in Form B. Additional logic within the step to use GFAPI::update_entry_property( $entry_id, 'workflow_step', 134 );would be required along with detecting the $status parameter matches your approve/reject target.
add_action( 'gravityflow_step_complete', 'jo_gravityflow_step_complete', 10, 4 );
function jo_gravityflow_step_complete( $step_id, $entry_id, $form_id, $status ) {
	/* For Reference:
		- actioning_entry = The entry which just completed it's form connector step.
		- connected_entry = The entry you want to be restarted.
	*/

	//Change 143 to match the step which you want completion action to occur with.
	if ( $step_id == '143' ) {
		$actioning_entry_id = $entry_id;
		$actioning_entry = GFAPI::get_entry( $actioning_entry_id );
		//Field 3 = Field that stores the entry ID which you want to update (i.e connected_entry_id)
		if ( ! is_wp_error( $actioning_entry ) && isset( $actioning_entry['3'] ) ) {
			$connected_entry_id = $actioning_entry['3'];
			$connected_entry = GFAPI::get_entry( $connected_entry_id );

			//Restart the step which has already had its' workflow_step meta value updated.
			$api = new Gravity_Flow_API( $connected_entry['form_id'] );
			$api->restart_step( $connected_entry );

			//--------------
			//Skip this block if you do not want to update timelines to note how/why.
			$connected_new_step = $api->get_current_step( $connected_entry );
			//Update actioning entry timeline
			gravity_flow()->add_timeline_note( $actioning_entry_id, 'This entry caused Form A Entry #' . $connected_entry_id . ' to move to Step "' . $connected_new_step->get_name() . '" and be (re)started.' );
			//Update connected entry timeline
			gravity_flow()->add_timeline_note( $connected_entry_id, 'This entry was moved to Step "' . $connected_new_step->get_name() . '" and (re)started due to Form B Entry #' . $actioning_entry_id . ' selection(s).' );
			//----------------
		}
	}
}

Placement

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