Enable “Add New” Option
Enable Advanced Select’s “Add New” option that allows users to create new items that aren’t in the initial list of options.
If the options are being populated from a taxonomy term using GPPA, enabling the insert_new_option
setting will create
a new taxonomy term.
Instructions
Install this snippet by following the instructions here: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
Customize the form_id and field_id properties at the bottom of this snippet.
Code
Filename: gpadvs-enable-add-new-option.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
* Gravity Perks // Advanced Select // Enable "Add New" Option
* https://gravitywiz.com/documentation/gravity-forms-advanced-select/
*
* Enable Advanced Select's "Add New" option that allows users to create new items that aren't in the initial list of options.
* If the options are being populated from a taxonomy term using GPPA, enabling the `insert_new_option` setting will create
* a new taxonomy term.
*
* Instructions:
*
* 1. Install this snippet by following the instructions here:
* https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
*
* 2. Customize the form_id and field_id properties at the bottom of this snippet.
*/
class GPASVS_Enable_Add_New_Option {
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_id' => false,
'insert_new_option' => false,
) );
// 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() {
if ( ! is_callable( 'gp_advanced_select' ) ) {
return;
}
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
// Advanced Select has to beat GF's default Chosen init script to the punch so uses `gform_pre_render` to register
// its init script early. We need to beat it to the punch, so we use the same filter on a higher priority.
add_filter( 'gform_pre_render', array( $this, 'add_init_script' ), 9, 2 );
add_filter( 'gform_pre_render', array( $this, 'allow_created_choices_in_save_and_continue' ), 10, 3 );
add_filter( 'gform_pre_render', array( $this, 'disable_state_validation_for_advanced_select_field' ), 10, 1 );
add_filter( 'gform_pre_render', array( $this, 'add_new_option_to_choices' ), 10, 1 );
add_action( 'gform_after_submission', array( $this, 'create_new_option' ), 10, 2 );
add_filter( 'gppa_input_choices', array( $this, 'enable_new_choice_for_gppa_empty' ), 10, 3 );
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
window.GPADVSEnableAddNewOption = function( args ) {
gform.addFilter( 'gpadvs_settings', function( settings, gpadvs ) {
if ( args.formId && gpadvs.formId != args.formId ) {
return settings;
}
if ( args.fieldId && gpadvs.fieldId != args.fieldId ) {
return settings;
}
settings.create = true;
/**
* Uncomment the below code to customize the display of the "Add New" option.
*/
// if ( ! settings.render ) {
// settings.render = {};
// }
// settings.render.option_create = function( data, escape ) {
// return '<div class="create">Add <strong>' + escape(data.input) + '</strong>…</div>';
// }
return settings;
} );
}
</script>
<?php
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $form;
}
$args = array(
'formId' => $this->_args['form_id'],
'fieldId' => $this->_args['field_id'],
);
$script = 'new GPADVSEnableAddNewOption( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( 'gpadvs_enable_add_new_option', $this->_args['form_id'], $this->_args['field_id'] ) );
GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
return $form;
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || (int) $form_id == (int) $this->_args['form_id'];
}
public function is_applicable_field( $field ) {
if ( ! gp_advanced_select()->is_advanced_select_field( $field ) ) {
return false;
}
// Check if this instance is targeting all Advanced Select fields or a specific field.
if ( ! empty( $this->_args['field_id'] ) && $this->_args['field_id'] != $field->id ) {
return false;
}
return true;
}
public function allow_created_choices_in_save_and_continue( $form, $ajax, $field_values ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $form;
}
foreach ( $form['fields'] as &$field ) {
if ( ! $this->is_applicable_field( $field ) ) {
continue;
}
$incomplete_submission_info = GFFormsModel::get_draft_submission_values( rgget( 'gf_token' ) );
if ( ! $incomplete_submission_info ) {
continue;
}
$submission_details = json_decode( $incomplete_submission_info['submission'], true );
/*
* If there is a value for this field in $field_values that is not present in $field->choices, add it and mark
* it as selected.
*/
$field_value = rgars( $submission_details, 'partial_entry/' . $field->id );
$choice_values = wp_list_pluck( $field->choices, 'value' );
if ( ! in_array( $field_value, $choice_values ) ) {
$field->choices[] = array(
'text' => $field_value,
'value' => $field_value,
'isSelected' => true,
);
}
}
return $form;
}
/**
* Add whatever new option the user entered to the list of choices so it is not removed on multi-page forms.
*
* @param array $form The form object currently being processed.
*
* @return array
*/
public function add_new_option_to_choices( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $form;
}
foreach ( $form['fields'] as &$field ) {
if ( ! $this->is_applicable_field( $field ) ) {
continue;
}
// Get the value of the field from the $_POST data.
$field_value = rgpost( 'input_' . $field->id );
if ( empty( $field_value ) ) {
continue;
}
// Convert non-multi select fields to an array of values so we can treat them all the same.
if ( $field->get_input_type() !== 'multiselect' ) {
$field_value = array( $field_value );
}
$existing_choice_values = wp_list_pluck( $field->choices, 'value' );
// Add the new options to the list of choices.
foreach ( $field_value as $value ) {
// If the value is already in the list of choices, don't add it again.
if ( in_array( $value, $existing_choice_values ) ) {
continue;
}
$field->choices[] = array(
'text' => $value,
'value' => $value,
'isSelected' => true,
);
}
}
return $form;
}
public function disable_state_validation_for_advanced_select_field( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $form;
}
foreach ( $form['fields'] as &$field ) {
if ( ! $this->is_applicable_field( $field ) ) {
continue;
}
$field->validateState = false;
}
return $form;
}
public function create_new_option( $entry, $form ) {
if (
! $this->is_applicable_form( $form ) ||
! $this->_args['insert_new_option'] ||
! class_exists( '\GP_Populate_Anything' )
) {
return;
}
foreach ( $form['fields'] as &$field ) {
if ( ! $this->is_applicable_field( $field ) || $field['gppa-choices-object-type'] != 'term' ) {
continue;
}
$tax_filter = false;
$choice_template_value = $field['gppa-choices-templates']['value'];
$filter_groups = $field['gppa-choices-filter-groups'];
if ( ! empty( $filter_groups ) ) {
foreach ( $filter_groups as $group_filter ) {
$tax_filter = current(
array_filter( $group_filter, function( $item ) {
return $item['property'] == 'taxonomy' && $item['operator'] == 'is';
} )
);
}
}
if ( ! $tax_filter || ! in_array( $choice_template_value, array( 'name', 'term_id', 'slug' ) ) ) {
return;
}
$field_value = rgar( $entry, $field->id );
$taxonomy = $tax_filter['value'];
// check if new option
$term = get_term_by( $choice_template_value, $field_value, $taxonomy );
if ( $term ) {
return;
}
$inserted_term = wp_insert_term( $field_value, $taxonomy );
if ( is_wp_error( $inserted_term ) ) {
return;
}
$term = get_term( $inserted_term['term_id'], $taxonomy );
// Update entry
gform_update_meta( $entry['id'], $field->id, $term->$choice_template_value, $form['id'] );
$gppa_choice_labels = array( $field->id => array( $term->$choice_template_value => $field_value ) );
gform_update_meta( $entry['id'], 'gppa_choices', $gppa_choice_labels, $form['id'] );
}
}
public function enable_new_choice_for_gppa_empty( $choices, $field, $objects ) {
$form = GFAPI::get_form( $field->formId );
if ( ! $this->is_applicable_form( $form ) || ! $this->is_applicable_field( $field ) ) {
return $choices;
}
if ( is_array( $choices ) && rgar( $choices[0], 'gppaErrorChoice' ) == 'no_choices' ) {
unset( $choices[0]['gppaErrorChoice'] );
}
return $choices;
}
}
# Configuration
new GPASVS_Enable_Add_New_Option(array(
'form_id' => 123,
// 'field_id' => 4,
));