bookmark_bordergform_export_field_value

Description

Use this filter override the field value before it is included in the CSV export. This filter can be used in conjunction with the gform_export_fields filter to include custom columns in the export.

Usage

add_filter( 'gform_export_field_value', 'your_function_name', 10, 4 );

Parameters

  • $value string

    Value of the field being exported

  • $form_id integer

    ID of the current form.

  • $field_id integer

    ID of the current field.

  • $entry Entry Object

    The current entry.

Examples

1. Set export value for custom fields

This example sets the values for two custom fields that were added to the export. The custom fields are named “custom_field1” and “custom_field2”.

add_filter( 'gform_export_field_value', 'set_export_values', 10, 4 );
function set_export_values( $value, $form_id, $field_id, $entry ) {
    switch( $field_id ) {
    case 'custom_field1' :
        $value = 'valueforcustomfield1';
        break;
    case 'custom_field2' :
        $value = 'valueforcustomfield2';
        break;
    }
    return $value;
}

2. Use choice text instead of values

add_filter( 'gform_export_field_value', 'export_choice_text', 10, 4 );
function export_choice_text( $value, $form_id, $field_id, $entry ) {
	$form  = GFAPI::get_form( $form_id );
	$field = RGFormsModel::get_field( $form, $field_id );

	return is_object( $field ) && is_array( $field->choices ) ? $field->get_value_export( $entry, $field_id, true ) : $value;
}

3. Decode special characters

The following example shows how you can decode special characters such as the ampersand which may have been encoded during field value sanitization.

add_filter( 'gform_export_field_value', 'decode_export_values' );
function decode_export_values( $value ) {

    return htmlspecialchars_decode( $value );
}

4. Format date field value

add_filter( 'gform_export_field_value', 'format_date', 10, 4 );
function format_date( $value, $form_id, $field_id, $entry ) {
	$form  = GFAPI::get_form( $form_id );
	$field = RGFormsModel::get_field( $form, $field_id );

	return is_object( $field ) && $field->type == 'date' ? GFCommon::date_display( $value, $field->dateFormat ) : $value;
}

5. Use display name instead of user id

add_filter( 'gform_export_field_value', 'export_user_display_name', 10, 4 );
function export_user_display_name( $value, $form_id, $field_id, $entry ) {
    if ( $field_id == 'created_by' ) {
        $user  = get_user_by( 'id', $value );

        return is_object( $user ) ? $user->display_name : $value;
    }

    return $value;
}

6. Format Number field value

add_filter( 'gform_export_field_value', 'format_number_field_value', 10, 4 );
function format_number_field_value( $value, $form_id, $field_id, $entry ) {
	$form  = GFAPI::get_form( $form_id );
	$field = RGFormsModel::get_field( $form, $field_id );
        $include_thousands_sep = true;

	return is_object( $field ) && $field->type == 'number' ? GFCommon::format_number( $value, $field->numberFormat, rgar( $entry, 'currency' ), $include_thousands_sep ) : $value;
}

7. Remove new lines, tabs and carriage returns

The following example shows how you can remove new lines, tabs and carriage returns before exporting the field value. Useful when importing CSV files in Excel.

add_filter( 'gform_export_field_value', 'decode_export_values' );
function decode_export_values( $value ) {
 	$value = str_replace(array("\n", "\t", "\r"), '', $value);
    return $value;
}

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GFExport::start_export() in export.php