Skip to content

gppa-force-gppa-on-save-and-continue.php: Added snippet to force GPPA value over a Save & Continue Value. #597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

saifsultanc
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2099104029/41936?folderId=3808239

Summary

When resuming a Save & Continue session, GPPA-populated fields correctly honor the Save & Continue value. This snippet would force GPPA to repopulate the field, ignoring the Save & Continue value.

The snippet in Action: https://www.loom.com/share/6ccde3a615234810b086a41f3c029f2d

Copy link
Contributor

@claygriffiths claygriffiths left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I'm digging the direction.

Just a few notes from me. (Also seeing PHPCS issues).

To do our diligence, did you first try the gppa_should_force_use_field_value filter?

$target_form_id = 29;
$target_field_id = 1;
static $_gppa_forcing_hydration;
if ( $form['id'] == $target_form_id && ! $_gppa_forcing_hydration ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a check for the field ID here? You might need to introduce a foreach as being able to specify multiple fields would be nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha!

$_gppa_forcing_hydration = true;
$submission = json_decode( $submission_json, ARRAY_A );
$field = GFAPI::get_field( $form, $target_field_id );
$hydrated_field = gp_populate_anything()->hydrate_field( $field, $form, array(), null, false );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing array() for field values, it might be helpful to pass the draft entry that way if something relies on another field's value to populate, it'll populate correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha!

@saifsultanc
Copy link
Contributor Author

@claygriffiths before I update on the current snippet. Here's the other one:

add_filter( 'gppa_should_force_use_field_value', function( $should_use_field_value, $field ) {

	if ( ! rgar( $_REQUEST, 'gf_token' ) ) {
		return $should_use_field_value;
	}

	$save_and_continue_values = gp_populate_anything()->get_save_and_continue_values( rgar( $_REQUEST, 'gf_token' ) );
	if ( empty( $save_and_continue_values ) ) {
		return $should_use_field_value;
	}

	foreach ( $save_and_continue_values as $input_id => &$value ) {
		if ( absint( $field->id ) === absint( $input_id ) ) {
			if ( is_array( $value ) ) {
				$value = array_filter( $value );
			}
			// Force GPPA to repopulate for a particular field ID.
			if ( absint( $input_id ) == 1 ) {
				$should_use_field_value = false;
			}
			break;
		}
	}

	return $should_use_field_value;
}, 10, 2 );

I tried with this snippet using the filter gppa_should_force_use_field_value, but that did not fetch any positive results. The only time it does the repopulation is when I update the GPPA field value to a blank value. Here is quick 30 sec summary screencast with this snippet: https://www.loom.com/share/3c1a6817f08840f6be06b6957b25aa55

@claygriffiths
Copy link
Contributor

@saifsultanc Got it! This is good to have on record. I'm cool with going forward with what you have in this PR.

@saifsultanc
Copy link
Contributor Author

@saifsultanc Got it! This is good to have on record. I'm cool with going forward with what you have in this PR.

Thanks @claygriffiths . Multiple target field support updated. Loomo ~45 sec : https://www.loom.com/share/d0aebbe72b0f426887884ad6c33c91f7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants