|
14 | 14 | add_action( 'wp_loaded', function() { |
15 | 15 |
|
16 | 16 | $form_id = 123; // Change this to the form's ID |
17 | | - $field_id = 4; // Change this to the Calculation field's ID. |
| 17 | + $field_id = 4; // Change this to the Calculation field's ID. |
18 | 18 |
|
19 | 19 | $values = array(); |
20 | 20 | $calculating = false; |
21 | 21 |
|
22 | 22 | add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values, $calculating ) { |
23 | | - if ( $calculating || $field['id'] !== $field_id ) { |
| 23 | + if ( $calculating ) { |
| 24 | + return $value; |
| 25 | + } |
| 26 | + |
| 27 | + if ( $field['id'] !== $field_id ) { |
24 | 28 | $values[ $field['id'] ] = $value; |
25 | 29 | return $value; |
26 | 30 | } |
27 | 31 |
|
28 | 32 | // Set flag to prevent infinite recursion. |
29 | 33 | $calculating = true; |
30 | 34 |
|
31 | | - $form = GFAPI::get_form( $entry['form_id'] ); |
32 | | - $_entry = $entry + $values; |
| 35 | + $form = GFAPI::get_form( $entry['form_id'] ); |
| 36 | + $_entry = $entry + $values; |
| 37 | + $calculated_value = GFCommon::calculate( $field, $form, $_entry ); |
| 38 | + |
| 39 | + GFAPI::update_entry_field( $_entry['id'], $field_id, $calculated_value ); |
33 | 40 |
|
34 | 41 | // Reset flag. |
35 | 42 | $calculating = false; |
36 | 43 |
|
37 | | - return GFCommon::calculate( $field, $form, $_entry ); |
| 44 | + return $calculated_value; |
38 | 45 | }, 10, 4 ); |
39 | 46 |
|
| 47 | + // GravityView Support. |
| 48 | + add_filter( 'gravityview/field/number/value', function( $value, $field, $view, $form, $entry ) use ( $field_id, $form_id ) { |
| 49 | + if ( $field->ID != $field_id && $form->ID != $form_id ) { |
| 50 | + return $value; |
| 51 | + } |
| 52 | + |
| 53 | + $orig_entry = GFAPI::get_entry( $entry->ID ); |
| 54 | + return rgar( $orig_entry, $field_id ); |
| 55 | + }, 10, 5 ); |
| 56 | + |
40 | 57 | } ); |
0 commit comments