gform_rule_pre_evaluation

Description

The gform_rule_pre_evaluation filters can be used to modify a conditional logic rule before it is evaulated.

Usage

The gform_rule_pre_evaluation filter has both a JavaScript version and a PHP version. Both versions should be used.

The JavaScript version only overrides the conditional logic rule on the front-end.

gform.addFilter( 'gform_rule_pre_evaluation', function( rule, formId, conditionalLogic ) {
    // Modify the rule.

    return rule;
} );

The PHP version overrides the conditional logic rule when it is evaulated during and post submission.

add_filter( 'gform_rule_pre_evaluation', function(  $rule, $form, $logic, $field_values, $entry ) {
    // Modify the rule.

    return $rule;
}, 10, 5 );

JavaScript Version

Parameters

  • rule Javascript Object

    The current rule object. e.g.

    {
      "fieldId": "1",
      "operator": "isnot",
      "value": ""
    }
    
  • formId integer|string

    The ID of the form in use.

  • conditionalLogic Javascript Object

    The current conditional logic object. e.g.

    {
      "actionType": "show",
      "logicType": "all",
      "rules": [
        {
          "fieldId": "1",
          "operator": "isnot",
          "value": ""
        },
        {
          "fieldId": "1",
          "operator": "is",
          "value": "{:1}"
        }
      ]
    }
    

Example

This example shows how you can replace field merge tags used in the conditional logic rule value.

gform.addFilter( 'gform_rule_pre_evaluation', function( rule, formId ) {
	var mergeTags = GFMergeTag.parseMergeTags( rule.value );
	if ( ! mergeTags.length ) {
		return rule;
	}

	rule.value = GFMergeTag.getMergeTagValue( formId, mergeTags[0][1], mergeTags[0][3] );

	return rule;
} );

Placement

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

Since

This filter was added in Gravity Forms v2.4.22.

Source Code

This filter is located in js/conditional_logic.js

PHP Version

Parameters

  • $rule array

    The current rule object. See the Rule Properties section of the Conditional Logic Object article.

  • $form Form Object

    The form currently being processed.

  • $logic Conditional Logic Object

    The conditional logic.

  • $field_values array

    An array of dynamic population parameter keys with their corresponding values to be populated.

  • $entry Entry Object

    The entry currently being processed, if available.

Example

This example shows how you can replace field merge tags used in the conditional logic rule value.

add_filter( 'gform_rule_pre_evaluation', 'filter_gform_rule_pre_evaluation', 10, 5 );
function filter_gform_rule_pre_evaluation( $rule, $form, $logic, $field_values, $entry ) {
	if ( ! empty( $rule['value'] ) && strpos( $rule['value'], '{' ) !== false ) {
		remove_filter( 'gform_rule_pre_evaluation', 'filter_gform_rule_pre_evaluation' );
		if ( empty( $entry ) ) {
			$entry = GFFormsModel::get_current_lead();
		}
		$rule['value'] = GFCommon::replace_variables( $rule['value'], $form, $entry, false, false, false, 'text' );
		add_filter( 'gform_rule_pre_evaluation', 'filter_gform_rule_pre_evaluation', 10, 5 );
	}

	return $rule;
}

Note: In this example field merge tags are being processed before the entry has been saved so GFFormsModel::get_current_lead() is used to get a mock entry object for the current submission and GFCommon::replace_variables() is used to process the merge tags. Both of these methods can trigger the evaulation of conditional logic which will trigger this filter. To prevent a recursion related fatal error you must remove your hooked function before calling those methods and restore it after.

Placement

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

Since

This filter was added in Gravity Forms v2.4.22.

Source Code

This filter is located in GFFormsModel::evaluate_conditional_logic() in forms_model.php