Multiple Source Form fields mapped to same Target Form Field.

If multiple Source Form Fields Mapped to the same Target Form Field, use a previous mapping rule in case the current one does not have any value.

Instructions

Code

Filename: gpep-map-multiple-source-fields-to-target-field.php

<?php
/**
 * Gravity Perks // Easy Passthrough // Multiple Source Form fields mapped to same Target Form Field.
 * https://gravitywiz.com/documentation/gravity-forms-easy-passthrough/
 *
 * Instruction Video: https://www.loom.com/share/d3ae6ba5370a44da9d6c16d36b048c5b
 *
 * If multiple Source Form Fields Mapped to the same Target Form Field, use a previous mapping rule in case the current one does not have any value.
 */
// Replace 2 with the target form ID and 1 with the target form field
add_filter( 'gpep_target_field_value_2_1', function( $field_value, $form_id, $target_field_id, $source_field ) {
	static $possible_gpep_field_values = array();

	// if current value is not empty, add to the static array
	if ( ! empty( $field_value ) ) {
		$possible_gpep_field_values[] = $field_value;
	} else {
		// if current value is empty, try to restore the last added value
		if ( ! empty( $possible_gpep_field_values ) ) {
			$field_value = end( $possible_gpep_field_values );
		}
	}
	return $field_value;
}, 10, 4 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.