Created
June 19, 2014 16:29
-
-
Save mikeselander/d9483a5c735b54eefde6 to your computer and use it in GitHub Desktop.
A function to automatically fill in an edit gravity form with the appropriate data from a custom table object
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
add_filter('gform_pre_render_3', 'populate_fields_contact'); // Document Form | |
function populate_fields_contact( $form ){ | |
// Make sure we're editing the object correctly | |
if ( $_GET['update_contact'] ){ | |
// Set our default values | |
$data_type = "contact"; | |
$data_type_length = 8; // 'contact_' = 8 char | |
// Run our query - I'm using a custom sql query class (EE_Query) since I'm reusing it multiple times | |
$args = array( | |
'type' => $data_type, | |
'log_id' => $_GET['update_contact'], | |
); | |
$contact = EE_Query::get_query( $args ); | |
$contact_object = $contact[0]; | |
// Loop through our resulting object set and re-assign into an easier array | |
foreach ( $contact_object as $key => $value ){ | |
$contact_key_array[] = $data_type."_".$key; | |
} | |
// Loop through all possible fields | |
foreach ( $form['fields'] as &$field ){ | |
// If our field is one of multiple input fields | |
if ( $field['type'] == 'name' || $field['type'] == 'address' ){ | |
// Loop through the inputs in the fields | |
foreach( $field['inputs'] as $input ){ | |
// Check to see if the dynamic population name matches our arry | |
if( in_array( $input["name"], $contact_key_array ) ) : | |
// If it does, strip the "_contact" prefix | |
$correct_key = substr( $input["name"], $data_type_length ); | |
// Set the field value to the correct value | |
$field['defaultValue']["$input[id]"] = $contact_object["$correct_key"]; | |
endif; | |
} | |
// If it is a normal field, the process is even easier | |
} else { | |
if( in_array( $field["inputName"], $contact_key_array ) ) : // $field["inputName"] == "contact_".$key | |
$correct_key = substr( $field["inputName"], $data_type_length ); | |
$field["defaultValue"] = $contact_object["$correct_key"]; | |
endif; | |
} | |
} | |
} | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment