Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save claygriffiths/e1b1dcb43fa9afb93633060aacb946b0 to your computer and use it in GitHub Desktop.
Save claygriffiths/e1b1dcb43fa9afb93633060aacb946b0 to your computer and use it in GitHub Desktop.
<?php
/**
* Gravity Perks // Nested Forms // Force {Parent} Merge Tag Replacement on Submission
* http://gravitywiz.com/documentation/gravity-forms-nested-forms/
*/
add_filter( 'gform_entry_post_save', function ( $entry, $form ) {
$pattern = '/{Parent\:(.+)}/i';
foreach ( $form['fields'] as &$field ) {
if ( $field->get_input_type() == 'form' ) {
$child_form_id = $field->gpnfForm;
$child_form = GFAPI::get_form( $child_form_id );
$child_entry_ids = explode( ',', rgar( $entry, $field->id ) );
foreach ( $child_form['fields'] as $child_field ) {
if ( is_array( $child_field->inputs ) ) {
foreach ( $child_field->inputs as $input ) {
if ( empty( $input['defaultValue'] ) ) {
continue;
}
preg_match( $pattern, $input['defaultValue'], $match );
if ( $match ) {
$value = rgar( $entry, $match[1] );
$input_id = implode( '.', array_slice( explode( '.', $match[1] ), 1 ) );
$child_entry_input = $child_field->id . '.' . $input_id;
foreach ( $child_entry_ids as $child_entry_id ) {
GFAPI::update_entry_field( $child_entry_id, $child_entry_input, $value );
}
}
}
} else {
preg_match( $pattern, $child_field->defaultValue, $match );
if ( $match ) {
$value = rgar( $entry, $match[1] );
foreach ( $child_entry_ids as $child_entry_id ) {
GFAPI::update_entry_field( $child_entry_id, $child_field->id, $value );
}
}
}
}
}
}
return $entry;
}, 11, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment