Changing values
So every once in a while you'll run in to a scenario, where the output of your Excel shouldn't be one-on-one from the
database. Perhaps you need to prefix the value, or rather then M
/F
you want it to say Male
/Female
. How would we do this?
** Example **
Building on the prefix example, we'll use the gfexcel_field_value
filter, or hook.
// some functions.php file
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 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.
add_filter('gfexcel_field_value', function ($value, $entry, $field) {
// $value is the string value of this field
// $entry is an array of the entire row in the Excel file
// $field is an instance of GF_Field
if ($field->formId !== 3 || $field->id !== 5) {
return $value;
}
return 'Prefixed: ' . $value;
}, 10, 3); // 3 refers to the number of arguments. This needs to be updated!
// is the same as (asuming it's a text field)
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 al that info.