GravityExport: How to output dropdown field labels Instead of values
By default, GravityExport outputs the stored value of dropdown fields, not the label that users see. This article shows you how to modify that behavior.
Read more about the gfexcel_export_field_value filter.
Option 1: All dropdown fields
Target all select/dropdown fields in the export:
/**
* Modify dropdown field export values to show labels instead of stored values.
*
* @since 1.0.0
*
* @param string $value The field value being exported.
* @param int $form_id The form ID.
* @param int $field_id The field ID.
* @param array $entry The entry data.
*
* @return string The modified field value.
*/
add_filter( 'gfexcel_export_field_value_select', function( $value, $form_id, $field_id, $entry ) {
// Get the form and field.
$form = GFAPI::get_form( $form_id );
$field = GFFormsModel::get_field( $form, $field_id );
// Get the display value (label) instead of the stored value.
if ( $field && 'select' === $field->get_input_type() ) {
$value = $field->get_value_export( $entry, $field_id, true );
}
return $value;
}, 10, 4 );
Option 2: A specific field
Target a specific dropdown field (e.g., field #5 on form #1):
/**
* Modify a specific dropdown field export value to show label instead of stored value.
*
* @since 1.0.0
*
* @param string $value The field value being exported.
* @param int $form_id The form ID.
* @param string $field_id The field ID.
* @param array $entry The entry data.
*
* @return string The modified field value.
*/
add_filter( 'gfexcel_export_field_value_select_1_5', function( $value, $form_id, $field_id, $entry ) {
// Get the form and field objects.
$form = GFAPI::get_form( $form_id );
$field = GFFormsModel::get_field( $form, $field_id );
// Return the label instead of the value.
return $field ? $field->get_value_export( $entry, $field_id, true ) : $value;
}, 10, 4 );
Option 3: Other field types
The same approach works for other choice-based fields:
/**
* Modify radio button field export values to show labels instead of stored values.
*
* @since 1.0.0
*
* @param string $value The field value being exported.
* @param int $form_id The form ID.
* @param int $field_id The field ID.
* @param array $entry The entry data.
*
* @return string The modified field value.
*/
add_filter( 'gfexcel_export_field_value_radio', function( $value, $form_id, $field_id, $entry ) {
$form = GFAPI::get_form( $form_id );
$field = GFFormsModel::get_field( $form, $field_id );
return $field ? $field->get_value_export( $entry, $field_id, true ) : $value;
}, 10, 4 );
/**
* Modify multi-select field export values to show labels instead of stored values.
*
* @since 1.0.0
*
* @param string $value The field value being exported.
* @param int $form_id The form ID.
* @param int $field_id The field ID.
* @param array $entry The entry data.
*
* @return string The modified field value.
*/
add_filter( 'gfexcel_export_field_value_multiselect', function( $value, $form_id, $field_id, $entry ) {
$form = GFAPI::get_form( $form_id );
$field = GFFormsModel::get_field( $form, $field_id );
return $field ? $field->get_value_export( $entry, $field_id, true ) : $value;
}, 10, 4 );