Skip to content

Instantly share code, notes, and snippets.

@travislopes
Created December 11, 2015 17:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save travislopes/f40ed846fb321088ed16 to your computer and use it in GitHub Desktop.
Save travislopes/f40ed846fb321088ed16 to your computer and use it in GitHub Desktop.
<?php
/**
* Gravity Forms Easy Form Population
*
* Populate form fields with values from a previous form entry.
*
* @version 1.0
* @author Travis Lopes <tl@travislop.es>
* @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();
}
@espermedia
Copy link

Found a workaround for my particular use of checkboxes, but am having another issue. If I want to dynamically populate form fields NOT from another form, this snippet seems to disable any other pre-population when $entry_id is not in the querystring and it returns $form. Is there a simple way to have the basic dynamic population from the querystring function when $entry_id is NOT being passed in? I have a field with dynamic population enabled via parameter 'tour' and if I try to simply load the form with ?tour=value the value is no longer received by the form and populated.

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