diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2023-03-07 22:33:23 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2023-03-07 22:33:23 +0100 |
commit | 30b731c4cf5a1233f926dea2ef643d9d920afd89 (patch) | |
tree | 9555846cae92eb1ac65f64cf0aeb5bf46485c64f /includes | |
parent | 711366c3512f79e2754b4bbc57dddd76e871267e (diff) | |
download | gigologadmin-30b731c4cf5a1233f926dea2ef643d9d920afd89.tar.gz gigologadmin-30b731c4cf5a1233f926dea2ef643d9d920afd89.tar.bz2 gigologadmin-30b731c4cf5a1233f926dea2ef643d9d920afd89.zip |
elect-field-helper can take array of single values as options.
Diffstat (limited to 'includes')
-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>"; } |