Skip to content

Commit ecb5560

Browse files
authored
Forms: Hide empty radio fields (#41379)
1 parent 9c55617 commit ecb5560

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Forms: Hide empty radio fields.

projects/packages/forms/src/contact-form/class-contact-form-field.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,10 @@ class="below-label__label ' . ( $this->is_error() ? ' form-error' : '' ) . '"
10001000
* @return string HTML
10011001
*/
10021002
public function render_field( $type, $id, $label, $value, $class, $placeholder, $required, $required_field_text ) {
1003+
if ( ! $this->is_field_renderable( $type ) ) {
1004+
return null;
1005+
}
1006+
10031007
$class .= ' grunion-field';
10041008

10051009
$form_style = $this->get_form_style();
@@ -1141,6 +1145,33 @@ private function maybe_override_type() {
11411145
return $type;
11421146
}
11431147

1148+
/**
1149+
* Determines if a form field is valid.
1150+
*
1151+
* Add checks here to confirm if any given form field
1152+
* is configured correctly and thus should be rendered
1153+
* on the frontend.
1154+
*
1155+
* @param string $type - the field type.
1156+
*
1157+
* @return bool
1158+
*/
1159+
public function is_field_renderable( $type ) {
1160+
// Check for valid radio field.
1161+
if ( $type === 'radio' ) {
1162+
$options = (array) $this->get_attribute( 'options' );
1163+
$non_empty_options = array_filter(
1164+
$options,
1165+
function ( $option ) {
1166+
return $option !== '';
1167+
}
1168+
);
1169+
return count( $non_empty_options ) > 0;
1170+
}
1171+
1172+
return true;
1173+
}
1174+
11441175
/**
11451176
* Gets the form style based on its CSS class.
11461177
*
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: bugfix
3+
4+
Forms: Hide empty radio fields.

0 commit comments

Comments
 (0)