Force Default Value
Force the default value to be captured for fields hidden by conditional logic.
Instructions
Code
Filename: gw-force-default-value.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Gravity Wiz // Gravity Forms // Force Default Value
* https://gravitywiz.com/
*
* Instruction Video: https://www.loom.com/share/ce1cee94063348f09d57b404d26c4e8a
*
* Force the default value to be captured for fields hidden by conditional logic. Also reprocesses default values that
* contain merge tags not available for prepopulation.
*
* The latter functionality is useful when wanting to combine data into specifically formatted values. For example, you
* can use "{entry:gpaa_lat_1},{entry:gpaa_lng_1}" to format the latitude and longitude captured by GP Address Autocomplete
* into a comma-delimited pair.
*
* Currently, this will only work with single input fields.
*
* Plugin Name: Gravity Forms - Force Default Value
* Plugin URI: https://gravitywiz.com/
* Description: Force the default value to be captured for fields hidden by conditional logic.
* Author: Gravity Wiz
* Version: 1.3
* Author URI: https://gravitywiz.com/
*/
class GW_Force_Default_Value {
private $_args = array();
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_ids' => array(),
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// make sure we're running the required minimum version of Gravity Forms
if ( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.9', '>=' ) ) {
return;
}
// carry on
add_filter( 'gform_entry_post_save', array( $this, 'add_default_values_to_entry' ), 10, 2 );
add_filter( 'gform_replace_merge_tags', array( $this, 'replace_unreplaced_merge_tags' ) );
}
public function add_default_values_to_entry( $entry, $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $entry;
}
$requires_update = false;
/** @var GF_Field $field */
foreach ( $form['fields'] as $field ) {
if ( ! $this->is_applicable_field( $field ) ) {
continue;
}
$entry_value = rgar( $entry, $field->id );
$is_hidden = GFFormsModel::is_field_hidden( $form, $field, array(), $entry );
// Get default value if field is hidden.
if ( $is_hidden || ( empty( $entry_value ) && ! GFFormDisplay::is_field_validation_supported( $field ) ) ) {
$value = $field->get_value_default_if_empty( $field->get_value_submission( array(), false ) );
} else {
$value = $entry_value;
}
if ( rgblank( $value ) ) {
continue;
}
$value = is_string( $value ) ? GFCommon::replace_variables( $value, $form, $entry ) : $value;
if ( $value != $entry_value ) {
$requires_update = true;
// For array based values like Date Field and Time Field.
if ( is_array( $value ) ) {
$value = $field->get_value_save_entry( $value, $form, null, null, null );
}
$entry[ $field->id ] = $value;
}
}
if ( $requires_update ) {
GFAPI::update_entry( $entry );
}
return $entry;
}
/**
* If default value *is* a merge tag (or multiple merge tags), and these merge tags still exist after prepopulation
* merge tags have been replaced, assume that this is a merge tag that should be replaced on submission and clear
* it from the default value so that the user does not see the merge tag if the field is visible.
*
* @param $text
*
* @return mixed|string
*/
public function replace_unreplaced_merge_tags( $text ) {
if ( isset( $_POST['gform_submit'] ) ) {
return $text;
}
$chars = str_split( trim( $text ) );
if ( rgar( $chars, 0 ) === '{' && rgar( $chars, count( $chars ) - 1 ) === '}' ) {
$text = '';
}
return $text;
}
function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
}
function is_applicable_field( $field ) {
return empty( $this->_args['field_ids'] ) || in_array( $field->id, $this->_args['field_ids'] );
}
}
# Configuration
new GW_Force_Default_Value( array(
'form_id' => 123,
'field_ids' => array( 4, 5 ),
) );
Can this be enabled in fields – date, time?
Hi,
Currently, it appears the snippet only works on single input fields, so it won’t work on those fields. I’ll pass this as a feature request for the snippet.
Best,