Use List Field as Choices for Gravity Forms
Adds support for populating choice-based fields with values entered in a List field.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Read the Walkthrough
Code
Filename: gw-list-field-as-choices.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
<?php
/**
* Gravity Wiz // Gravity Forms // Use List Field as Choices for Gravity Forms
* https://gravitywiz.com/use-list-field-choices-gravity-forms/
*
* Adds support for populating choice-based fields (i.e. checkboxes, selects, radio buttons) with values entered in a
* List field. This functionality requires that the form has multiple pages and that the List field must be placed on
* a page prior to the choice-based field that it will populate.
*
* Plugin Name: Gravity Forms - Use List Field as Choices
* Plugin URI: https://gravitywiz.com/use-list-field-choices-gravity-forms/
* Description: Adds support for populating choice-based fields with values entered in a List field.
* Author: Gravity Wiz
* Version: 1.0
* Author URI: https://gravitywiz.com/
*/
class GW_List_Field_As_Choices {
private $_args = array();
function __construct( $args ) {
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'list_field_id' => false,
'choice_field_ids' => false,
'label_template' => '{0}',
'sort' => false,
) );
if ( ! is_array( $this->_args['choice_field_ids'] ) ) {
$this->_args['choice_field_ids'] = array( $this->_args['choice_field_ids'] );
}
// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
extract( $this->_args ); // gives us $form_id, $list_field_id, $choices_field_id
add_filter( 'gform_pre_render', array( $this, 'populate_choice_fields' ), 9 );
add_filter( 'gform_pre_validation', array( $this, 'populate_choice_fields' ), 9 );
add_filter( 'gform_pre_submission_filter_' . $form_id, array( $this, 'populate_choice_fields' ) );
}
function populate_choice_fields( $form ) {
if ( $form['id'] != $this->_args['form_id'] ) {
return $form;
}
$list_field = GFFormsModel::get_field( $form, $this->_args['list_field_id'] );
$values = GFFormsModel::get_field_value( $list_field );
/**
* Filter the values from the List field that will be used to populate field choices.
*
* @param array|mixed|string $values The List field values that will be used to populate field choices.
* @param array $form The current form.
* @param array $args The arguments used to initialize this instance of GW_List_Field_As_Choices.
*/
$values = apply_filters( 'gwlfac_list_field_values', $values, $form, $this->_args );
// if list field doesn't have any values, let's ditch this party
if ( ! is_array( $values ) ) {
return $form;
}
$choices = $this->get_list_choices( $values );
foreach ( $form['fields'] as &$field ) {
if ( ! $this->is_applicable_field( $field ) ) {
continue;
}
// set 'choices' for choice fields
$field['choices'] = $choices;
// only set inputs for 'checkbox' choice fields
if ( GFFormsModel::get_input_type( $field ) == 'checkbox' ) {
$inputs = array();
foreach ( $choices as $index => $choice ) {
$inputs[] = array(
'label' => $choice['text'],
'id' => $field['id'] . '.' . ( $index + 1 ),
);
}
$field['inputs'] = $inputs;
}
}
return $form;
}
function get_list_choices( $values ) {
$choices = array();
foreach ( $values as $row ) {
$label = $this->replace_template( $this->_args['label_template'], $row );
$value = isset( $this->_args['value_template'] ) ? $this->replace_template( $this->_args['value_template'], $row ) : $label;
$choices[] = array(
'text' => $label,
'value' => $value,
);
}
if ( $this->_args['sort'] == true ) {
usort( $choices, function( $a, $b ) {
return strnatcasecmp( $a['text'], $b['text'] );
} );
}
return $choices;
}
function replace_template( $template, $row ) {
// combine our templates so we can find all matches at once
preg_match_all( '/{(\w+)}/', $template, $matches, PREG_SET_ORDER );
if ( is_array( $row ) ) {
$mega_row = array_merge( $row, array_values( $row ) );
foreach ( $matches as $match ) {
$template = str_replace( $match[0], rgar( $mega_row, $match[1] ), $template );
}
} else {
foreach ( $matches as $match ) {
$template = str_replace( $match[0], $row, $template );
}
}
return $template;
}
function is_applicable_field( $field ) {
$is_choice_field = is_array( rgar( $field, 'choices' ) );
$is_registered_field = in_array( $field['id'], $this->_args['choice_field_ids'] );
return $is_choice_field && $is_registered_field;
}
}
/**
* Uncomment the code below by removing the pound symbols (#) in front of each line. See @link in the comment section
* at the top for additional usage instructions.
*/
# Basic Usage
//new GW_List_Field_As_Choices( array(
// 'form_id' => 1,
// 'list_field_id' => 2,
// 'choice_field_ids' => 3,
//) );
# Enable Sorting of Choices Alphanumerically
//new GW_List_Field_As_Choices( array(
// 'form_id' => 1,
// 'list_field_id' => 2,
// 'choice_field_ids' => 3,
// 'sort' => true,
//) );
# Populating Multiple Choice Fields
//new GW_List_Field_As_Choices( array(
// 'form_id' => 384,
// 'list_field_id' => 3,
// 'choice_field_ids' => array( 6, 7 ),
//) );
# Customizing the Choice Label and Value
//new GW_List_Field_As_Choices( array(
// 'form_id' => 384,
// 'list_field_id' => 2,
// 'choice_field_ids' => array( 4, 5 ),
// 'label_template' => '{Name} <span style="color:#999;font-style:italic;">({Age})</span>',
// 'value_template' => '{Name}',
//) );
# Filter Usage
## Customize List field values to be populated as choices based on Gravity Flow User Input step.
//add_filter( 'gwlfac_list_field_values', function( $values, $form, $args ) {
// if ( is_array( $values ) ) {
// return $values;
// }
# Confirm we are within a Gravity Flow Inbox.
// if ( rgget( 'lid' ) && rgget( 'page' ) == 'gravityflow-inbox' ) {
// $entry = GFAPI::get_entry( (int) rgget( 'lid' ) );
// // Verify the entry list field has previously stored values to use.
// if ( $entry ) {
// $values = unserialize( $entry[ $args['list_field_id'] ] );
// if ( ! is_array( $values ) ) {
// return false;
// } else {
// return $values;
// }
// }
// }
// return false;
//}, 10, 3 );
Is it possible to do this code snippet in a form, with one page (if it helps, I’m using JetSloth’s accordion and the list and checkbox fields appear in two accordions) Thanks for all your lovely code snippets.
Hey there,
Glad to hear that you are enjoying the snippets!
Just gave this one a test, and can confirm that this snippet is compatible with JetSloth’s Collapsible Sections plugin (v1.3.3). In any case (accordions or not), please note that this snippet requires your form to have multiple pages, and that the List field must be placed on a page prior to the choice-based field that it will populate.
“Thank you for your response. My question is, can the code snippet be adapted for a single-page form?”
Hi,
The snippet in its current state can’t be configured to work on a single page form. It may however be possible with some customization. You can submit a feature request for this via our support form, so our developers can look into this further.
Best,