Choice Groups

Organize Drop Down and Multi Select choices into visual groups using special heading choices.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gw-choice-groups.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
<?php
/**
 * Gravity Wiz // Gravity Forms // Choice Groups
 * https://gravitywiz.com/
 *
 * Organize Drop Down and Multi Select choices into visual groups using special heading choices.
 *
 * Example:
 *
 * [Category 1]
 * First Choice
 * Second Choice
 * [Category 2]
 * Choice A
 * Choice B
 *
 * Supported Fields:
 * - Drop Down
 * - Multi Select
 *
 * Plugin Name:  Gravity Forms - Choice Groups for Drop Down and Multi Select
 * Plugin URI:   https://gravitywiz.com/
 * Description:  Organize Drop Down and Multi Select choices into visual groups using special heading choices.
 * Author:       Gravity Wiz
 * Version:      0.1
 * Author URI:   https://gravitywiz.com/
 */

class GW_Choice_Groups {

	const GROUP_PATTERN = '/^\[(.+)\]$/';

	public function __construct() {
		add_filter( 'gform_field_input', array( $this, 'field_input' ), 10, 5 );
	}

	public function field_input( $input, $field, $value, $entry_id, $form_id ) {

		if ( ! in_array( $field->type, array( 'select', 'multiselect' ), true ) ) {
			return $input;
		}

		if ( ! $this->has_groups( $field->choices ) ) {
			return $input;
		}

		$is_multiselect = $field->type === 'multiselect';
		$groups         = $this->get_groups( $field->choices );

		$field_id = (int) $field->id;

		$select_attributes = array(
			sprintf( 'name="%s"', esc_attr( $is_multiselect ? 'input_' . $field_id . '[]' : 'input_' . $field_id ) ),
			sprintf( 'id="input_%d_%d"', $form_id, $field_id ),
			sprintf( 'class="%s"', esc_attr( $is_multiselect ? 'gfield_multiselect' : 'medium gfield_select' ) ),
		);

		if ( $is_multiselect ) {
			$select_attributes[] = 'multiple="multiple"';
			$select_attributes[] = 'size="5"';
		}

		$tabindex = $field->get_tabindex();

		if ( $tabindex ) {
			$select_attributes[] = trim( $tabindex );
		}

		$input = sprintf(
			'<select %s>',
			implode( ' ', $select_attributes )
		);

		if ( ! $is_multiselect && ! empty( $field->placeholder ) ) {
			$input .= sprintf(
				'<option value="">%s</option>',
				esc_html( $field->placeholder )
			);
		}

		foreach ( $groups as $group ) {

			if ( isset( $group['label'] ) ) {

				$input .= sprintf(
					'<optgroup label="%s">',
					esc_attr( $group['label'] )
				);

				foreach ( $group['choices'] as $choice ) {
					$input .= $this->get_option_markup( $choice, $value );
				}

				$input .= '</optgroup>';

			} else {

				$input .= $this->get_option_markup( $group, $value );

			}
		}

		$input .= '</select>';

		return $input;

	}

	private function has_groups( $choices ) {

		foreach ( $choices as $choice ) {

			if ( preg_match( self::GROUP_PATTERN, trim( rgar( $choice, 'text' ) ) ) ) {
				return true;
			}
		}

		return false;

	}

	private function get_groups( $choices ) {

		$groups = array();

		$current_group_index = null;

		foreach ( $choices as $choice ) {

			$text = trim( rgar( $choice, 'text' ) );

			if ( preg_match( self::GROUP_PATTERN, $text, $matches ) ) {

				$groups[] = array(
					'label'   => trim( $matches[1] ),
					'choices' => array(),
				);

				$current_group_index = count( $groups ) - 1;

				continue;

			}

			if ( $current_group_index !== null ) {
				$groups[ $current_group_index ]['choices'][] = $choice;
			} else {
				$groups[] = $choice;
			}
		}

		return $groups;

	}

	private function get_option_markup( $choice, $value ) {

		$choice_value = rgar( $choice, 'value' );

		if ( $choice_value === '' || $choice_value === null ) {
			$choice_value = rgar( $choice, 'text' );
		}

		$selected = '';

		if ( is_array( $value ) ) {

			if ( in_array( (string) $choice_value, array_map( 'strval', $value ), true ) ) {
				$selected = ' selected="selected"';
			}
		} elseif ( (string) $choice_value === (string) $value ) {
			$selected = ' selected="selected"';
		}

		return sprintf(
			'<option value="%s"%s>%s</option>',
			esc_attr( $choice_value ),
			$selected,
			esc_html( rgar( $choice, 'text' ) )
		);

	}

}

new GW_Choice_Groups();

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.