Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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

This comment has been minimized.

Copy link

espermedia commented Mar 4, 2016

Thanks for the code snippet. Any chance of getting this updated to also handle checkbox fields? I see it handles multi-value inputs, but checkboxes have choices and inputs...

@espermedia

This comment has been minimized.

Copy link

espermedia commented Mar 11, 2016

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
You can’t perform that action at this time.