Skip to content

Commit

Permalink
mvc: OptionField - add support for the optgroup in a one level deep n…
Browse files Browse the repository at this point in the history
…esting construction. This offers the ability to use the optgroup frontend glue added in 9206823 using an OptionField definition like:

<field type="OptionField">
    <OptionValues>
        <opt1 value='option group 1'>
           <opt1 value='option1'>option 1</opt1>
        </opt1>
        <option_group2>
           <opt2>option 2</option2>
        </option_group2>
    </OptionValues>
</field>
  • Loading branch information
AdSchellevis committed Oct 15, 2023
1 parent 09cabd9 commit bc19530
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Expand Up @@ -99,18 +99,24 @@ public function getNodeData()
$result = array();
// if option is not required, add empty placeholder
if (!$this->internalIsRequired && !$this->internalMultiSelect) {
$result[""] = array("value" => $this->internalEmptyDescription, "selected" => empty((string)$this->internalValue) ? 1 : 0);
$result[""] = [
"value" => $this->internalEmptyDescription,
"selected" => empty((string)$this->internalValue) ? 1 : 0
];
}

// explode options
$options = explode(',', $this->internalValue);
foreach ($this->internalOptionList as $optKey => $optValue) {
if (in_array($optKey, $options)) {
$selected = 1;
$selected = in_array($optKey, $options) ? 1 : 0;
if (is_array($optValue) && isset($optValue['value'])) {
// option container (multiple attributes), passthrough.
$result[$optKey] = $optValue;
} else {
$selected = 0;
// standard (string) option
$result[$optKey] = ["value" => $optValue];
}
$result[$optKey] = array("value" => $optValue, "selected" => $selected);
$result[$optKey]["selected"] = $selected;
}

return $result;
Expand Down
Expand Up @@ -34,20 +34,33 @@
*/
class OptionField extends BaseListField
{
/**
* @var string static option list, no need to parse the same structure multiple times
*/
private static $internalCacheOptionList = [];

/**
* setter for option values
* @param $data
*/
public function setOptionValues($data)
{
if (is_array($data)) {
$this->internalOptionList = array();
if (is_array($data) && empty(self::$internalCacheOptionList)) {
self::$internalCacheOptionList = [];
// copy options to internal structure, make sure we don't copy in array structures
foreach ($data as $key => $value) {
if (!is_array($value)) {
$this->internalOptionList[$key] = gettext($value);
self::$internalCacheOptionList[$key] = gettext($value);
} else {
foreach ($value as $subkey => $subval) {
self::$internalCacheOptionList[$subkey] = [
'value' => $subval,
'optgroup' => $key
];
}
}
}
}
$this->internalOptionList = self::$internalCacheOptionList;
}
}

0 comments on commit bc19530

Please sign in to comment.