Permalink
Browse files

feature(views): add attributes to input select options

Added the ability to pass attributes for the input select. When
in "options" or "option_values" is passed an array as string name,
the "text" key is used for the name/text and all other elements in
the array are added as attribute to the select.
  • Loading branch information...
Wouter van Os
Wouter van Os committed Oct 23, 2015
1 parent ed21578 commit 63b04d6ab7af21c43df97efc04012c7575889cd5
Showing with 33 additions and 4 deletions.
  1. +33 −4 views/default/input/select.php
@@ -9,11 +9,16 @@
* @subpackage Core
*
* @uses $vars['value'] The current value or an array of current values if multiple is true
* @uses $vars['options'] An array of strings representing the options for the dropdown field
* @uses $vars['options'] An array of strings or arrays representing the options
* for the dropdown field. If an array is passed,
* the "text" key is used as its text, all other
* elements in the array are used as attributes.
* @uses $vars['options_values'] An associative array of "value" => "option"
* where "value" is the name and "option" is
* the value displayed on the button. Replaces
* $vars['options'] when defined.
* $vars['options'] when defined. When "option"
* is passed as an array, the same behaviour is used
* as when the $vars['options'] is passed an array to.
* @uses $vars['multiple'] If true, multiselect of values will be allowed in the select box
* @uses $vars['class'] Additional CSS class
*/
@@ -59,15 +64,39 @@
'selected' => in_array((string)$opt_value, $value),
);
$options_list .= elgg_format_element('option', $option_attrs, $option);
if (is_array($option)) {
$text = elgg_extract('text', $option, '');
unset($option['text']);
if (!$text) {
elgg_log('No text defined for input/select option with value "' . $opt_value . '"', 'ERROR');
}
$option_attrs = array_merge($option_attrs, $option);
} else {
$text = $option;
}
$options_list .= elgg_format_element('option', $option_attrs, $text);
}
} else {
if (is_array($options)) {
foreach ($options as $option) {
$option_attrs = ['selected' => in_array((string)$option, $value)];
$options_list .= elgg_format_element('option', $option_attrs, $option);
if (is_array($option)) {
$text = elgg_extract('text', $option, '');
unset($option['text']);
if (!$text) {
elgg_log('No text defined for input/select option', 'ERROR');
}
$option_attrs = array_merge($option_attrs, $option);
} else {
$text = $option;
}
$options_list .= elgg_format_element('option', $option_attrs, $text);
}
}
}

0 comments on commit 63b04d6

Please sign in to comment.