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.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us