-
-
Save spivurno/32e914d67723f89717be2904ce6234c4 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Gravity Wiz // Gravity Forms // Populate Form with Entry (Optionally Update Entry on Submission) | |
* | |
* Pass an entry ID and populate the form automatically. No form configuration required. Optionally update the entry on | |
* submission. | |
* | |
* @version 1.5 | |
* @author David Smith <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/ | |
* @see https://www.screencast.com/t/SdNyfbNyC5C | |
* | |
* Plugin Name: Gravity Forms - Populate Form w/ Entry | |
* Plugin URI: http://gravitywiz.com/ | |
* Description: Pass an entry ID and populate the form automatically. No form configuration required. Optionally update the entry on submission. | |
* Author: Gravity Wiz | |
* Version: 1.4 | |
* Author URI: http://gravitywiz.com | |
*/ | |
class GW_Populate_Form { | |
public function __construct( $args = array() ) { | |
$this->_args = wp_parse_args( $args, array( | |
'form_id' => false, | |
'query_arg' => 'eid', | |
'update' => false, | |
) ); | |
add_action( 'init', array( $this, 'init' ) ); | |
} | |
public function init() { | |
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) { | |
return; | |
} | |
add_filter( 'gform_form_args', array( $this, 'prepare_field_values_for_population' ) ); | |
add_filter( 'gform_entry_id_pre_save_lead', array( $this, 'set_submission_entry_id' ), 10, 2 ); | |
} | |
public function prepare_field_values_for_population( $args ) { | |
if( ! $this->is_applicable_form( $args['form_id'] ) || ! $this->get_entry_id() ) { | |
return $args; | |
} | |
$entry = GFAPI::get_entry( $this->get_entry_id() ); | |
if( is_wp_error( $entry ) ) { | |
return $args; | |
} | |
$args['field_values'] = $this->prepare_entry_for_population( $entry ); | |
add_filter( sprintf( 'gform_pre_render_%d', $args['form_id'] ), array( $this, 'prepare_form_for_population' ) ); | |
return $args; | |
} | |
public function prepare_form_for_population( $form ) { | |
foreach( $form['fields'] as &$field ) { | |
$field['allowsPrepopulate'] = true; | |
if( is_array( $field['inputs'] ) ) { | |
$inputs = $field['inputs']; | |
foreach( $inputs as &$input ) { | |
$input['name'] = (string) $input['id']; | |
} | |
$field['inputs'] = $inputs; | |
} | |
$field['inputName'] = $field['id']; | |
} | |
return $form; | |
} | |
public function prepare_entry_for_population( $entry ) { | |
$form = GFFormsModel::get_form_meta( $entry['form_id'] ); | |
foreach( $form['fields'] as $field ) { | |
if( $field->type == 'post_category' ) { | |
$value = explode( ':', $entry[ $field->id ] ); | |
$entry[ $field->id ] = $value[1]; | |
} | |
switch( GFFormsModel::get_input_type( $field ) ) { | |
case 'checkbox': | |
$values = $this->get_field_values_from_entry( $field, $entry ); | |
if( is_array( $values ) ) { | |
$value = implode( ',', array_filter( $values ) ); | |
} else { | |
$value = $values; | |
} | |
$entry[$field['id']] = $value; | |
break; | |
case 'list': | |
$value = maybe_unserialize( rgar( $entry, $field->id ) ); | |
$list_values = array(); | |
if( is_array( $value ) ) { | |
foreach( $value as $vals ) { | |
if( is_array( $vals ) ) { | |
$vals = implode( '|', array_map( function( $value ) { | |
$value = str_replace( ',', ',', $value ); | |
return $value; | |
}, $vals ) ); | |
} | |
array_push( $list_values, $vals ); | |
} | |
$entry[ $field->id ] = implode( ',', $list_values ); | |
} | |
break; | |
} | |
} | |
return $entry; | |
} | |
public function get_field_values_from_entry( $field, $entry ) { | |
$values = array(); | |
foreach( $entry as $input_id => $value ) { | |
$fid = intval( $input_id ); | |
if( $fid == $field['id'] ) | |
$values[] = $value; | |
} | |
return count( $values ) <= 1 ? $values[0] : $values; | |
} | |
public function set_submission_entry_id( $entry_id, $form ) { | |
if( ! $this->_args['update'] || ! $this->is_applicable_form( $form['id'] ) || ! $this->get_entry_id() ) { | |
return $entry_id; | |
} | |
add_filter( 'gform_use_post_value_for_conditional_logic_save_entry', '__return_true' ); | |
return $this->get_entry_id(); | |
} | |
public function get_entry_id() { | |
return rgget( $this->_args['query_arg'] ); | |
} | |
public function is_applicable_form( $form ) { | |
$form_id = isset( $form['id'] ) ? $form['id'] : $form; | |
return ! $this->_args['form_id'] || $form_id == $this->_args['form_id']; | |
} | |
} | |
# Configuration | |
new GW_Populate_Form( array( | |
'update' => true | |
) ); |
I tried this script, but it doesn't seem to save changes I make to radio buttons. I wonder if it's a problem because of the version of Gravity Forms I'm using which is 2.3.1.
UPDATE 6/1/2018:
Thank you to David Smith for version 1.3. It fixed the problem I was having with the radio buttons not updating.
works fine for me, but I don´t manage to get it to work together with the plugin gravity wiz gp limit submissions. Only counts one.
I noticed that this doesn't seem to work with file upload fields...
Could this be modified to automatically update an existing entry, by putting the entry ID into a hidden field?
Your code works perfect but it does not show file uploads when editing gravity form. Can you help? Let me know.
Thanks for this wonderfull script. I only have one question. It loads the data up really nice but it doesnt reload the checkbox answers. Is this something I can fix myself?
@ginodp Possibly something AI could fix for you! This is pretty old code though. If you wanted a supported solution, Easy Passthrough is the ticket.
https://gravitywiz.com/documentation/gravity-forms-easy-passthrough/
This could potentially be a security hole if implemented incorrectly (someone can retrieve form data holding sensitive information). I would advise anyone utilising this to use it in conjunction with Gravity Perks Unique ID plugin. The code will require some modification to check for unique ID field.
Excellent snippet nevertheless; thank you, David.