Description
This filter can be used to modify the value returned by a field merge tag before it is used by a number field calculation or calculated product field.
Usage
The gform_merge_tag_value_pre_calculation filter has both a JavaScript version and a PHP version. Both versions should be used.
The JavaScript version only overrides the merge tag value on the front-end.
gform.addFilter( 'gform_merge_tag_value_pre_calculation', function( value, mergeTagArr, isVisible, formulaField, formId ) {
// do stuff
return result;
} );
The PHP version overrides the merge tag value when the calculation is rerun during submission using the field values saved in the entry.
add_filter( 'gform_merge_tag_value_pre_calculation', function( $value, $input_id, $modifier, $field, $form, $entry ) {
// do stuff
return $result;
}, 10, 5 );
JavaScript Version
Parameters
| Parameter | Type | Description |
|---|---|---|
value | float | The field value after the merge tag was processed. |
mergeTagArr | array | The merge tag being processed. |
isVisible | boolean | Is the field referenced in the merge tag visible or hidden by conditional logic? |
formulaField | Javascript Object | The current calculation field object. |
formId | integer | The ID of the form in use. |
mergeTagArr[0] contains the full merge tag e.g. {Field label:ID:modifier}mergeTagArr[1] contains the field or input ID e.g. 2mergeTagArr[3] contains the modifier(s) including the colon e.g. :valuemergeTagArr[4] contains the modifier(s) without the colon e.g. value
The current calculation field object. e.g.
{"field_id":3,"formula":"{:1}+{:2}","rounding":""}
Enable the :value modifier for choice based pricing fields.
This example shows how you can enable the :value modifier for use with choice based pricing fields.
gform.addFilter('gform_merge_tag_value_pre_calculation', function (value, mergeTagArr, isVisible, formulaField, formId) {
var inputId = mergeTagArr[1],
fieldId = parseInt(inputId),
modifier = mergeTagArr[4],
field = jQuery('#field_' + formId + '_' + fieldId);
if (modifier == 'value' && field.hasClass('gfield_price') && isVisible) {
var input = field.find('input[name="input_' + inputId + '"], select[name="input_' + inputId + '"]');
// filter out unselected radio or checkbox inputs
if (input.length > 1 || input.prop('type') == 'checkbox') {
input = input.filter(':checked');
}
if (input.length > 0) {
var val = input.val().split('|');
if (val.length > 1) {
value = val[0];
}
}
}
return value;
});
Placement
Reference the article Adding JavaScript Code to the Frontend of Your Site.
Source Code
This filter is located in js/gravityforms.js
PHP Version
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | float | The field value after the merge tag was processed. |
$input_id | string | The field or input ID from the merge tag. |
$modifier | string | The modifier from the merge tag e.g. value. |
$field | Field Object | The calculation field currently being processed. |
$form | Form Object | The form currently being processed. |
$entry | Entry Object | The entry currently being processed. |
Enable the :value modifier for choice based pricing fields.
This example shows how you can enable the :value modifier for use with choice based pricing fields.
add_filter( 'gform_merge_tag_value_pre_calculation', function ( $value, $input_id, $modifier, $field, $form, $entry ) {
if ( $modifier == 'value' ) {
$value = GFCommon::to_number( GFCommon::replace_variables( "{:{$input_id}:value}", $form, $entry ) );
if ( ! $value || ! is_numeric( $value ) ) {
$value = 0;
}
}
return $value;
}, 10, 6 );
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Source Code
This filter is located in GFCommon::calculate() in common.php