MDL-82146 core_output: Add disabled parameter to select element

This commit is contained in:
Amaia Anabitarte 2024-07-05 10:14:24 +02:00
parent 554a790bf0
commit ec050bbee7
2 changed files with 20 additions and 6 deletions

View file

@ -0,0 +1,7 @@
issueNumber: MDL-82146
notes:
core_courseformat:
- message: >-
New $disabled parameter has been added to select, select_optgroup and
select_option html_writers to create disabled option elements.
type: improved

View file

@ -257,6 +257,7 @@ class html_writer {
* @param string|array $selected value or array of values depending on multiple attribute
* @param array|bool|null $nothing add nothing selected option, or false of not added
* @param null|array $attributes html select element attributes
* @param array $disabled An array of disabled options.
* @return string HTML fragment
*/
public static function select(
@ -265,7 +266,8 @@ class html_writer {
$selected = '',
$nothing = ['' => 'choosedots'],
?array $attributes = null,
) {
array $disabled = [],
): string {
$attributes = (array)$attributes;
if (is_array($nothing)) {
foreach ($nothing as $k => $v) {
@ -314,9 +316,9 @@ class html_writer {
foreach ($options as $value => $label) {
if (is_array($label)) {
// Ignore key, it just has to be unique.
$output .= self::select_optgroup(key($label), current($label), $selected);
$output .= self::select_optgroup(key($label), current($label), $selected, $disabled);
} else {
$output .= self::select_option($label, $value, $selected);
$output .= self::select_option($label, $value, $selected, $disabled);
}
}
return self::tag('select', $output, $attributes);
@ -328,14 +330,18 @@ class html_writer {
* @param string $label The label to display as the option.
* @param string|int $value The value the option represents
* @param array $selected An array of selected options
* @param array $disabled An array of disabled options.
* @return string HTML fragment
*/
private static function select_option($label, $value, array $selected) {
private static function select_option($label, $value, array $selected, array $disabled = []): string {
$attributes = [];
$value = (string)$value;
if (in_array($value, $selected, true)) {
$attributes['selected'] = 'selected';
}
if (in_array($value, $disabled, true)) {
$attributes['disabled'] = 'disabled';
}
$attributes['value'] = $value;
return self::tag('option', $label, $attributes);
}
@ -346,16 +352,17 @@ class html_writer {
* @param string $groupname The label to use for the group
* @param array $options The options in the group
* @param array $selected An array of selected values.
* @param array $disabled An array of disabled options.
* @return string HTML fragment.
*/
private static function select_optgroup($groupname, $options, array $selected) {
private static function select_optgroup($groupname, $options, array $selected, array $disabled = []): string {
if (empty($options)) {
return '';
}
$attributes = ['label' => $groupname];
$output = '';
foreach ($options as $value => $label) {
$output .= self::select_option($label, $value, $selected);
$output .= self::select_option($label, $value, $selected, $disabled);
}
return self::tag('optgroup', $output, $attributes);
}