Skip to content

Commit b127894

Browse files
authored
gw-show-unselected-choices.php: Added new snippet to show unselected choices alongside selected choices for Checkbox and Radio Button fields.
1 parent 4eac0cd commit b127894

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Gravity Wiz // Gravity Forms // Show Unselected Choices
4+
* https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
5+
*
6+
* Show unselectecd choices alongside selected choices in your Checkbox and Radio Button field output.
7+
* This includes support for the {all_fields} merge tag, individual merge tags, and columns in Entry Blocks.
8+
*
9+
* Instructions:
10+
*
11+
* 1. Install this snippet.
12+
* https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets
13+
*/
14+
add_filter( 'gform_merge_tag_filter', function( $value, $input_id, $modifier, $field, $raw_value, $format ) {
15+
if ( in_array( $field->get_input_type(), array( 'checkbox', 'radio' ) ) ) {
16+
$value = gpeb_show_unselected_choices( $field, $raw_value );
17+
}
18+
return $value;
19+
}, 10, 6 );
20+
21+
add_filter( 'gpeb_checkbox_display_value', function( $value, $field, $form, $entry ) {
22+
return gpeb_show_unselected_choices( $field, GFFormsModel::get_lead_field_value( $entry, $field ) );
23+
}, 10, 4 );
24+
25+
add_filter( 'gpeb_radio_display_value', function( $value, $field, $form, $entry ) {
26+
return gpeb_show_unselected_choices( $field, GFFormsModel::get_lead_field_value( $entry, $field ) );
27+
}, 10, 4 );
28+
29+
function gpeb_show_unselected_choices( $field, $raw_value ) {
30+
31+
$output = array();
32+
33+
if ( ! empty( $field->inputs ) ) {
34+
foreach ( $field->inputs as $input ) {
35+
$class = 'gpeb-unselected';
36+
$icon = '';
37+
if ( ! rgblank( $raw_value[ $input['id'] ] ) ) {
38+
$icon = '';
39+
$class = 'gpeb-selected';
40+
}
41+
$output[] = sprintf( '<li class="%s">%s %s</li>', $class, $icon, $input['label'] );
42+
}
43+
} else {
44+
foreach ( $field->choices as $choice ) {
45+
$class = 'gpeb-unselected';
46+
$icon = '';
47+
if ( $choice['value'] === $raw_value ) {
48+
$icon = '';
49+
$class = 'gpeb-selected';
50+
}
51+
$output[] = sprintf( '<li class="%s">%s %s</li>', $class, $icon, $choice['text'] );
52+
}
53+
}
54+
55+
return sprintf( '<ul class="gpeb-show-unselected-choices">%s</ul>', implode( "\n", $output ) );
56+
}

0 commit comments

Comments
 (0)