diff options
-rw-r--r-- | includes/view-helpers/select-field-helper.php | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/includes/view-helpers/select-field-helper.php b/includes/view-helpers/select-field-helper.php index c25244b..6aabb65 100644 --- a/includes/view-helpers/select-field-helper.php +++ b/includes/view-helpers/select-field-helper.php @@ -15,12 +15,13 @@ namespace EternalTerror\ViewHelpers; /** * Return HTML code for a selction box with the given options and preselected value. * - * @param string $name The name attribute for the selection box - * @param array $opts The options as arrays of [value, label] pairs + * @param string $name The name attribute for the selection box. + * @param array $opts The options as arrays of [value, label] pairs. An array + * of single values are also acceptable. * @param mixed|int $selected The value of the preselected option, or null if no * option is preselected. * @param string $blank Text to use for "no selection", defaults to "Please - * select..." + * select...". * @return string */ function select_field( @@ -30,8 +31,16 @@ function select_field( string $blank = 'Please select...' ) : string { $body = "<option value=\"\">{$blank}</option>"; foreach ( $opts as $opt ) { - $sel = selected( $selected, $opt[0], false ); - $body .= "<option value=\"{$opt[0]}\"{$sel}>{$opt[1]}</option>"; + if ( is_array( $opt ) ) { + $value = $opt[0]; + $desc = $opt[1] ?? $opt[0]; + } else { + $value = $opt; + $desc = $opt; + } + + $sel = selected( $selected, $value, false ); + $body .= "<option value=\"{$value}\"{$sel}>{$desc}</option>"; } return "<select name=\"{$name}\">{$body}</select>"; } |