From 30b731c4cf5a1233f926dea2ef643d9d920afd89 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 7 Mar 2023 22:33:23 +0100 Subject: elect-field-helper can take array of single values as options. --- includes/view-helpers/select-field-helper.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'includes') 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 = ""; foreach ( $opts as $opt ) { - $sel = selected( $selected, $opt[0], false ); - $body .= ""; + if ( is_array( $opt ) ) { + $value = $opt[0]; + $desc = $opt[1] ?? $opt[0]; + } else { + $value = $opt; + $desc = $opt; + } + + $sel = selected( $selected, $value, false ); + $body .= ""; } return ""; } -- cgit v1.2.3