-
-
Save lukecav/c64cab0a0355c99949cae828f5185b14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Gravity Forms Easy Form Population | |
| * | |
| * Populate form fields with values from a previous form entry. | |
| * | |
| * @version 1.0 | |
| * @author Travis Lopes <[email protected]> | |
| * @license GPL-2.0+ | |
| * @link http://travislop.es/ | |
| */ | |
| class Gravity_Forms_Easy_Form_Population { | |
| public function __construct() { | |
| add_filter( 'gform_field_value', array( $this, 'populate_fields' ), 10, 3 ); | |
| } | |
| public function populate_fields( $value, $field, $name ) { | |
| /* If the entry ID parameter is not set, return the form. */ | |
| if ( ! rgget( 'entry_id' ) ) { | |
| return $form; | |
| } | |
| /* Get the entry and its form. */ | |
| $original_entry = GFAPI::get_entry( rgget( 'entry_id' ) ); | |
| $original_form = GFAPI::get_form( $original_entry['form_id'] ); | |
| $field_values = array(); | |
| /* Loop through the original form's fields and push set values to field values array. */ | |
| foreach ( $original_form['fields'] as $field ) { | |
| /* If this field has multiple inputs, loop through them. */ | |
| if ( $field->inputs ) { | |
| foreach ( $field->inputs as $input ) { | |
| if ( $input['name'] ) { | |
| $field_values[ $input['name'] ] = rgar( $original_entry, $input['id'] ); | |
| } | |
| } | |
| } else { | |
| if ( $field->inputName ) { | |
| $field_values[ $field->inputName ] = rgar( $original_entry, $field->id ); | |
| } | |
| } | |
| } | |
| /* Return the field value. */ | |
| return rgar( $field_values, $name ) ? rgar( $field_values, $name ) : $value; | |
| } | |
| } | |
| if ( class_exists( 'GFForms' ) ) { | |
| new Gravity_Forms_Easy_Form_Population(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment