Changing values in GravityExport
So every once in a while, youโll run in to a scenario where the output of your export shouldnโt be the same as what is stored in the database. Perhaps you need to prefix the value, or rather then AM ย ย ย /PM ย ย ย ย you want it to say Morning/Afternoon. How would we do this? We use the gfexcel_export_field_value filter.
Filter Documentation #
The filter hook follows this pattern:
gfexcel_export_field_value_{field_type}_{form_id}_{field_id}- Base filter:
gfexcel_export_field_valueย – All values - Field type-specific:
gfexcel_export_field_value_{field_type}ย ย – All fields of that type - Form-specific:
gfexcel_export_field_value_{field_type}_{form_id}ย ย – All fields of that type on a specific form - Field-specific:
gfexcel_export_field_value_{field_type}_{form_id}_{field_id}ย ย – One specific field
Parameters:
$valueย ย (mixed) – The current field value being exported$form_idย ย (int) – The ID of the Gravity Forms form$field_idย ย (string|int) – The ID of the field being exported$entryย ย (array) – The complete entry array from Gravity Forms
Example #
Building on the prefix example, weโll use the gfexcel_field_value ย filter, or hook.
add_filter('gfexcel_field_value', function ($value) {
return 'Prefixed: ' . $value;
});Field specific #
This previous example isnโt the greatest, because now most your values will be prefixed. So rather then doing this programmatically using the other values given to the filter, we can extend the filter itself. Letโs append the fieldtype to the filter, and try again.
add_filter('gfexcel_field_value_text', function ($value) {
return 'Prefixed: ' . $value;
});By adding the _text ย ย ย ย part, we only target text fields, so all the other fields are untouched. Pretty neat, right? But still this isnโt useful in most cases. Why then provide it? Because the $value ย ย ย ย isnโt the only variable available.
Filter signature #
As shown, you donโt need to implement all options, but they have to be in this sequence. So if you need the field_id ย ย ย , youโll also need to provide the form_id . Thatโs actually a good thing, seeing as the field_id ย ย ย ย isnโt unique, but only unique to the form.
So how do you know what your form_id ย and field_id ย is? You can read them of the page as you are editing the form.
Next to the form title there is a ID: n ย ย ย ย tag. That contains the id for the form. The same goes for every field; when you hover your mouse over it, youโll see something like Field ID n ย ย ย .
As for the available variables, you need to update the entire add_filter ย ย ย ย function, to accept all variables.
/**
* Filters the GFExcel field value for a specific field.
*
* @param string $value The string value of this field.
* @param array $entry The entire entry/row in the exported file.
* @param GF_Field $field The field instance currently being processed.
*
* @return string Filtered field value with a prefix applied when matching form and field IDs.
*/
add_filter( 'gfexcel_field_value', function( $value, $entry, $field ) {
// Only modify field 5 on form 3.
if ( 3 !== (int) $field->formId || 5 !== (int) $field->id ) {
return $value;
}
return 'Prefixed: ' . $value;
}, 10, 3 );
/**
* Filters the GFExcel value for a specific text field (Form 3, Field 5).
*
* @param string $value The field value before export.
*
* @return string Modified value with a prefix applied.
*/
add_filter( 'gfexcel_field_value_text_3_5', function( $value ) {
return 'Prefixed: ' . $value;
} );As you can see, the filter is really powerful, and has a lot of data you can use to base your decisions on. You can even use other data from the row, as $entry ย ย ย ย contains all that info.