/**
* Method to get the field option groups.
*
* @return array The field option objects as a nested array in groups.
*
* @since 1.7.0
* @throws \UnexpectedValueException
*/
protected function getGroups()
{
$groups = array();
$label = 0;
foreach ($this->element->children() as $element) {
switch ($element->getName()) {
// The element is an <option />
case 'option':
// Initialize the group if necessary.
if (!isset($groups[$label])) {
$groups[$label] = array();
}
$disabled = (string) $element['disabled'];
$disabled = $disabled === 'true' || $disabled === 'disabled' || $disabled === '1';
// Create a new option object based on the <option /> element.
$tmp = HTMLHelper::_('select.option', $element['value'] ? (string) $element['value'] : trim((string) $element), Text::alt(trim((string) $element), preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname)), 'value', 'text', $disabled);
// Set some option attributes.
$tmp->class = (string) $element['class'];
// Set some JavaScript option attributes.
$tmp->onclick = (string) $element['onclick'];
// Add the option.
$groups[$label][] = $tmp;
break;
// The element is a <group />
case 'group':
// Get the group label.
if ($groupLabel = (string) $element['label']) {
$label = Text::_($groupLabel);
}
// Initialize the group if necessary.
if (!isset($groups[$label])) {
$groups[$label] = array();
}
// Iterate through the children and build an array of options.
foreach ($element->children() as $option) {
// Only add <option /> elements.
if ($option->getName() !== 'option') {
continue;
}
$disabled = (string) $option['disabled'];
$disabled = $disabled === 'true' || $disabled === 'disabled' || $disabled === '1';
// Create a new option object based on the <option /> element.
$tmp = HTMLHelper::_('select.option', $option['value'] ? (string) $option['value'] : Text::_(trim((string) $option)), Text::_(trim((string) $option)), 'value', 'text', $disabled);
// Set some option attributes.
$tmp->class = (string) $option['class'];
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
// Add the option.
$groups[$label][] = $tmp;
}
if ($groupLabel) {
$label = \count($groups);
}
break;
// Unknown element type.
default:
throw new \UnexpectedValueException(sprintf('Unsupported element %s in GroupedlistField', $element->getName()), 500);
}
}
reset($groups);
return $groups;
}