gravityflow_step_complete

The gravityflow_step_complete action fires on the completion of any step in a workflow except the complete step. For it, you would probably want to hook to gravityflow_workflow_complete, or possibly gravityflow_post_process_workflow.

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).' );
			//----------------
		}
	}
}

Example 4 - Following the approval of one child form entry, send any others to a different step.

This snippet was originally built to check the approval step (91) after the completion of a Form Connector Update an Entry step (92) and send the non-approved entries (which had the same parent entry as the approved) to a different notification step (93). This allowed the FC Update an Entry step to update the parent entry with the approved child entry ID before the additional child entries were moved. You will likely want to customize it to your use case.

add_action( 'gravityflow_step_complete', 'jo_gravityflow_step_complete', 10, 4 );
function jo_gravityflow_step_complete( $step_id, $entry_id, $form_id, $status ) {

	//Change 92 to match the step ID which you want completion action to occur with.
	if ( $step_id == '92' ) {
	
		//Change this to match your child form approval step ID;
		$approval_step_id = '91';
		$rejected_notification_step_id = '93';

		//Change this to match your parent form ID;
		$parent_form_id = '22';
		$parent_meta_key = 'workflow_parent_form_id_' . $parent_form_id . '_entry_id';

		$entry = GFAPI::get_entry( $entry_id );
		
		//Change the 22 in workflow_parent_form_id_22_entry_id to match your parent form ID
		if ( ! is_wp_error( $entry ) && isset( $entry[ $parent_meta_key ] ) ) {
			$parent_entry_id = $entry[ $parent_meta_key ];
			//$parent_entry = GFAPI::get_entry( $parent_entry_id );

			$search_criteria['field_filters'][] = array( 'key' => $parent_meta_key, 'value' => $parent_entry_id );
			$total_count    = 0;
			$matched_entries = GFAPI::get_entries( $form_id, $search_criteria, array(), array( 'offset' => 0, 'page_size' => 500 ), $total_count );

			if( ! empty( $matched_entries ) ) {
				foreach( $matched_entries as $matched_entry ) {
					if ( $matched_entry['id'] != $entry_id && $matched_entry[ 'workflow_step_status_' . $approval_step_id ] == 'pending' ) {
						$api = new Gravity_Flow_API( $form_id );
						$api->send_to_step( $matched_entry, $rejected_notification_step_id );
					}
				}
			}			
		}
	}
}

Placement

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