Skip to content

Commit 73f2cdd

Browse files
committed
gpdtc-recalc.php: Added GravityView Support.
1 parent 99b672c commit 73f2cdd

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

gp-date-time-calculator/gpdtc-recalc.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,44 @@
1414
add_action( 'wp_loaded', function() {
1515

1616
$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.
1818

1919
$values = array();
2020
$calculating = false;
2121

2222
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 ) {
2428
$values[ $field['id'] ] = $value;
2529
return $value;
2630
}
2731

2832
// Set flag to prevent infinite recursion.
2933
$calculating = true;
3034

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 );
3340

3441
// Reset flag.
3542
$calculating = false;
3643

37-
return GFCommon::calculate( $field, $form, $_entry );
44+
return $calculated_value;
3845
}, 10, 4 );
3946

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+
4057
} );

0 commit comments

Comments
 (0)