bookmark_bordergform_merge_tag_value_pre_calculation

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

  • value float

    The field value after the merge tag was processed.

  • mergeTagArr array

    The merge tag being processed.

    mergeTagArr[0] contains the full merge tag e.g. {Field label:ID:modifier}
    mergeTagArr[1] contains the field or input ID e.g. 2
    mergeTagArr[3] contains the modifier(s) including the colon e.g. :value
    mergeTagArr[4] contains the modifier(s) without the colon e.g. value

  • isVisible boolean

    Is the field referenced in the merge tag visible or hidden by conditional logic?

  • formulaField JavaScript Object

    The current calculation field object. e.g.

    {"field_id":3,"formula":"{:1}+{:2}","rounding":""}

  • formId integer

    The ID of the form in use.

Example

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

Your code snippet can be placed in a HTML field on your form or in a theme custom JavaScript file.

Source Code

This filter is located in js/gravityforms.js

PHP Version

Parameters

  • $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

    The form currently being processed.

  • $entry Entry Object

    The entry currently being processed.

Example

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

Your code snippet should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GFCommon::calculate() in common.php