I’m trying to get the values for checkboxes selected in a field to aggregate / implode into a single value to have a single column rather than multiple columns for export.
I initially tried this snippet:
/**
* Gravity Wiz // Gravity Forms // Export Multi-input Fields in a Single Column
* Plugin Name: Gravity Forms - Export Multi-input Fields in Single Column
* Plugin URI: http://gravitywiz.com/how-do-i-export-multi-input-fields-in-a-single-column-with-gravity-forms/
* Description: Export multi-input Gravity Forms fields as a single column.
* Author: David Smith
* Version: 1.2
* Author URI: http://gravitywiz.com
*/
add_filter( 'gform_export_fields', function( $form ) {
// only modify the form object when the form is loaded for field selection; not when actually exporting
if ( rgpost( 'export_lead' ) || rgpost( 'action' ) == 'gf_process_export' ) {
return $form;
}
$fields = array();
foreach ( $form['fields'] as $field ) {
if ( is_a( $field, 'GF_Field' ) && is_array( $field->inputs ) ) {
$orig_field = clone $field;
$field->inputs = null;
$fields[] = $field;
$fields[] = $orig_field;
} else {
$fields[] = $field;
}
}
$form['fields'] = $fields;
return $form;
} );
*/
Which is great, but the single column it creates for export has the same field ID as the checkboxes, so if I use gform_export_fields to remove the checkboxes from the form export, the aggregated single column is removed as well.
Right now I’m using this snippet to send the checkbox values to a separate hidden field value with merge tags before submitting:
/**
* Gravity Wiz // Gravity Forms // Map Submitted Field Values to Another Field
*
* Multi-input Fields (i.e. Name, Address, etc)
*
* To map the first and last name of a Name field to a single field,
* follow the steps above and enter {First Name:1.3} {Last Name:1.6}.
*
* @version 1.1
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @copyright 2014 Gravity Wiz
*/
class GWMapFieldToField {
public $lead = null;
function __construct( ) {
add_filter( 'gform_pre_validation', array( $this, 'map_field_to_field' ), 11 );
}
function map_field_to_field( $form ) {
foreach( $form['fields'] as $field ) {
if( is_array( $field['inputs'] ) ) {
$inputs = $field['inputs'];
} else {
$inputs = array(
array(
'id' => $field['id'],
'name' => $field['inputName']
)
);
}
foreach( $inputs as $input ) {
$value = rgar( $input, 'name' );
if( ! $value )
continue;
$post_key = 'input_' . str_replace( '.', '_', $input['id'] );
$current_value = rgpost( $post_key );
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $input['name'], $matches, PREG_SET_ORDER );
// if there is no merge tag in inputName - OR - if there is already a value populated for this field, don't overwrite
if( empty( $matches ) )
continue;
$entry = $this->get_lead( $form );
foreach( $matches as $match ) {
list( $tag, $field_id, $input_id, $filters, $filter ) = array_pad( $match, 5, 0 );
$force = $filter === 'force';
$tag_field = RGFormsModel::get_field( $form, $field_id );
// only process replacement if there is no value OR if force filter is provided
$process_replacement = ! $current_value || $force;
if( $process_replacement && ! RGFormsModel::is_field_hidden( $form, $tag_field, array() ) ) {
$field_value = GFCommon::replace_variables( $tag, $form, $entry );
if( is_array( $field_value ) ) {
$field_value [] = array_filter($field_value);
//$field_value = implode( ',' , $array);
}
} else {
$field_value = '';
}
$value = trim( str_replace( $match[0], $field_value, $value ) );
}
if( $value ) {
$_POST[$post_key] = $value;
}
}
}
return $form;
}
function get_lead( $form ) {
if( ! $this->lead )
$this->lead = GFFormsModel::create_lead( $form );
return $this->lead;
}
}
new GWMapFieldToField();
Which also works great, but 1. doesn’t export the imploded array with commas, and 2. if the user makes an error (eg not filling out a required field) on the initial submit, and then has to go back and fix the form and resubmit, those values are not added to the hidden field value.
Is there any way to combine the two? ie send the imploded array value from “Export Multi-input Fields in a Single Column” to a separate hidden field value after export?
That way I can remove the individual checkbox values / columns from the form export, but still have the single aggregated column, with commas, and won’t lose the values if the user has to resubmit the form?
Thanks!!