Skip to content

Commit 3324f97

Browse files
authored
gpdtc-recalc.php: Added GravityView Support.
1 parent f21a9a1 commit 3324f97

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,43 @@
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();
20-
$calculating = false;
20+
$calculating = false; // Flag to prevent infinite recursion
2121

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 ) {
22+
add_filter( sprintf( 'gform_get_input_value_%s', $form_id ), function( $value, $entry, $field, $input_id ) use ( $field_id, &$values, &$calculating ) {
23+
// Prevent infinite recursion
24+
if ( $calculating ) {
25+
return $value;
26+
}
27+
28+
if ( $field['id'] !== $field_id ) {
2429
$values[ $field['id'] ] = $value;
2530
return $value;
2631
}
2732

28-
// Set flag to prevent infinite recursion.
33+
// Set flag to prevent recursion
2934
$calculating = true;
3035

3136
$form = GFAPI::get_form( $entry['form_id'] );
3237
$_entry = $entry + $values;
38+
$calculated_value = GFCommon::calculate( $field, $form, $_entry );
3339

34-
// Reset flag.
40+
GFAPI::update_entry_field( $_entry['id'], $field_id, $calculated_value );
41+
// 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+
$orig_entry = GFAPI::get_entry( $entry->ID );
53+
return rgar( $orig_entry, $field_id );
54+
}, 10, 5 );
55+
4056
} );

0 commit comments

Comments
 (0)