Hi all, I have a tricky issue/question about repeater fields…
I’m trying to pre-populate a form from entry data, that contains a set of nested repeater fields. I’ve got it working with the code below, except it doesn’t have the data properyl when submitted. I can see the problem is to do with the input field names/IDs as these are normally set as a numerical array, whereas with my prepoluated version, all instances have the same name/ID (0).
Does anyone know the right way to set the names/IDs with the correct array keys?
<?php
// Populate repeater fields
function populate_repeater_on_edit($form){
// Check if we're editing the form entry
if(isset($_GET['gform_post_id']) && $_GET['gform_post_id'] != ''){
// Get WPDB for database queries
global $wpdb;
// Get the post ID from the URL
$post_id = $_GET['gform_post_id'];
// Get the GF entry data
$entry_id = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE `post_id` = $post_id && `meta_key` = '_gform-entry-id'");
$entry_id = $entry_id[0]->meta_value;
$entry = GFAPI::get_entry($entry_id);
// Loop through form fields
foreach($form['fields'] as $field){
// If we're on our repeater field
if($field->id == '1000'){
// Grab details of all the labels and description to use when creating new repeater entires
$labels = array(
'element_label' => $field->fields[0]->label,
'element_description' => $field->fields[0]->description,
'facts_label' => $field->fields[1]->label,
'facts_description' => $field->fields[1]->description,
'facts_addButtonText' => $field->fields[1]->addButtonText,
'facts_removeButtonText' => $field->fields[1]->removeButtonText,
'fact_label' => $field->fields[1]->fields[0]->label,
'fact_description' => $field->fields[1]->fields[0]->description,
'evidences_label' => $field->fields[1]->fields[1]->label,
'evidences_description' => $field->fields[1]->fields[1]->description,
'evidences_addButtonText' => $field->fields[1]->fields[1]->addButtonText,
'evidences_removeButtonText' => $field->fields[1]->fields[1]->removeButtonText,
'evidence_label' => $field->fields[1]->fields[1]->fields[0]->label,
'evidence_description' => $field->fields[1]->fields[1]->fields[0]->description
);
// Set an empty array to use for our repeater field data
$populated_fields = array();
// Loop through each 'Element' in our entry data
foreach($entry['1000'] as $entry_element){
// Create the 'Element' and add it to our array
$element = GF_Fields::create(array(
'type' => 'text',
'id' => 1001,
'formId' => $form['id'],
'label' => $labels['element_label'],
'pageNumber' => 1,
'isRequired' => true,
'defaultValue' =>$entry_element['1001']
));
$populated_fields[] = $element;
// Set an empty array to contain 'Facts' data
$facts_data = array();
// Loop through each 'Fact' and add it to our array
foreach($entry_element['1002'] as $entry_facts){
$fact = GF_Fields::create(array(
'type' => 'text',
'id' => 1003,
'formId' => $form['id'],
'label' => $labels['fact_label'],
'description' => $labels['fact_description'],
'pageNumber' => 1,
'isRequired' => true,
'defaultValue' => $entry_facts['1003']
));
$facts_data[] = $fact;
// Set an empty array to contain 'Evidence' data
$evidences_data = array();
// Loop through each 'Evidence' and add it to our array
foreach($entry_facts['1004'] as $entry_evidence){
$evidence = GF_Fields::create(array(
'type' => 'text',
'id' => 1005,
'formId' => $form['id'],
'label' => $labels['evidence_label'],
'description' => $labels['evidence_description'],
'pageNumber' => 1,
'isRequired' => true,
'defaultValue' => $entry_evidence['1005']
));
$evidences_data[] = $evidence;
}
// Add the 'Evidence' data to the 'Facts' data
$evidences = GF_Fields::create(array(
'type' => 'repeater',
'id' => 1004,
'formId' => $form['id'],
'label' => $labels['evidences_label'],
'addButtonText' => $labels['evidences_addButtonText'],
'removeButtonText' => $labels['evidences_removeButtonText'],
'pageNumber' => 1,
'fields' => $evidences_data,
));
$facts_data[] = $evidences;
}
// Add the 'Facts' data to the 'Elements' data
$facts = GF_Fields::create(array(
'type' => 'repeater',
'id' => 1002,
'formId' => $form['id'],
'label' => $labels['facts_label'],
'addButtonText' => $labels['facts_addButtonText'],
'removeButtonText' => $labels['facts_removeButtonText'],
'pageNumber' => 1,
'fields' => $facts_data,
));
$populated_fields[] = $facts;
}
// Add the 'Elements' data to the form
$field->fields = $populated_fields;
}
}
}
// Return the form data
return $form;
}
add_filter('gform_pre_render_35', 'populate_repeater_on_edit');
?>
Thanks,
Owen.