Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions libraries/src/HTML/Helpers/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public static function groupedlist($data, $name, $options = [])
// Set default options and overwrite with anything passed in
$options = array_merge(
HTMLHelper::$formatOptions,
['format.depth' => 0, 'group.items' => 'items', 'group.label' => 'text', 'group.label.toHtml' => true, 'id' => false],
['format.depth' => 0, 'group.items' => 'items', 'group.label' => 'text', 'group.attr' => 'attr', 'group.class' => 'class', 'group.label.toHtml' => true, 'id' => false],
$options
);

Expand Down Expand Up @@ -228,9 +228,11 @@ public static function groupedlist($data, $name, $options = [])
$groupIndent = str_repeat($options['format.indent'], $options['format.depth']++);

foreach ($data as $dataKey => $group) {
$label = $dataKey;
$id = '';
$noGroup = \is_int($dataKey);
$label = $dataKey;
$id = '';
$groupClass = '';
$groupAttribs = '';
$noGroup = \is_int($dataKey);

if ($options['group.items'] == null) {
// Sub-list is an associative array
Expand All @@ -248,6 +250,16 @@ public static function groupedlist($data, $name, $options = [])
$id = $group[$options['group.id']];
$noGroup = false;
}

if (isset($options['group.class'], $group[$options['group.class']])) {
$groupClass = $group[$options['group.class']];
$noGroup = false;
}

if (isset($options['group.attr'], $group[$options['group.attr']])) {
$groupAttribs = $group[$options['group.attr']];
$noGroup = false;
}
} elseif (\is_object($group)) {
// Sub-list is in a property of an object
$subList = $group->{$options['group.items']};
Expand All @@ -261,16 +273,39 @@ public static function groupedlist($data, $name, $options = [])
$id = $group->{$options['group.id']};
$noGroup = false;
}

if (isset($options['group.class'], $group->{$options['group.class']})) {
$groupClass = $group->{$options['group.class']};
$noGroup = false;
}

if (isset($options['group.attr'], $group->{$options['group.attr']})) {
$groupAttribs = $group->{$options['group.attr']};
$noGroup = false;
}
} else {
throw new \RuntimeException('Invalid group contents.', 1);
}

if ($noGroup) {
$html .= static::options($subList, $options);
} else {
$html .= $groupIndent . '<optgroup' . (empty($id) ? '' : ' id="' . $id . '"') . ' label="'
. ($options['group.label.toHtml'] ? htmlspecialchars($label, ENT_COMPAT, 'UTF-8') : $label) . '">' . $options['format.eol']
. static::options($subList, $options) . $groupIndent . '</optgroup>' . $options['format.eol'];
if (\is_array($groupAttribs)) {
$groupAttribs = ArrayHelper::toString($groupAttribs);
}

$html .= $groupIndent
. '<optgroup'
. (empty($id) ? '' : ' id="' . $id . '"')
. ' label="' . ($options['group.label.toHtml'] ? htmlspecialchars($label, ENT_COMPAT, 'UTF-8') : $label) . '"'
. (empty($groupClass) ? '' : ' class="' . $groupClass . '"')
. (empty($groupAttribs) ? '' : ' ' . $groupAttribs)
. '>'
. $options['format.eol']
. static::options($subList, $options)
. $groupIndent
. '</optgroup>'
. $options['format.eol'];
}
}

Expand Down