contao-component-style-manager
contao-component-style-manager copied to clipboard
Unable to create <optgroup> with current code
Hello,
I am unable to create select
s with optgroup
. And I think it's impossible to, according to the ComponentStyleSelect
widget's current code.
I managed to make it work with those slight modifications (starting line 140) :
foreach ($opts as $opt) {
$arrFieldOption = array(
'label' => $opt['value'] ?: $opt['key'],
'value' => $opt['key']
);
// if 'value' is an array, we format the subvalues the same way
if(is_array($arrFieldOption['value'])){
foreach($arrFieldOption['value'] as $arrFieldSuvOption){
$arrFieldOption['value'][] = array(
'label' => $arrFieldSuvOption['value'] ?: $arrFieldSuvOption['key'],
'value' => $arrFieldSuvOption['key']
);
}
}
$arrFieldOptions[] = $arrFieldOption;
}
// set options
$strFieldId = $this->strId . '_' . $objStyleGroups->id;
$strFieldName = $this->strName . '[' . $objStyleGroups->id . ']';
foreach ($arrFieldOptions as $strKey=>$arrOption)
{
if (!is_array($arrOption['value'])) // if not an array, we want to create a single <option>. Otherwise, it's an <optgroup>
{
// nothing changed here, keep original code
}
else
{
$arrOptgroups = array();
foreach ($arrOption['value'] as $arrOptgroup)
{
$arrOptgroups[] = sprintf('<option value="%s"%s>%s</option>',
StringUtil::specialchars($arrOptgroup['value']),
// @deprecated: to be removed in Version 3.0. (interception of storage based on the alias. In future, only the ID must be set)
static::optionSelected($arrOptgroup['value'], $this->varValue[ $objStyleGroups->id ] ?? '') ?: static::optionSelected($arrOptgroup['value'], $this->varValue[ $objStyleGroups->alias ] ?? ''),
$arrOptgroup['label']);
}
$arrOptions[] = sprintf('<optgroup label=" %s">%s</optgroup>', StringUtil::specialchars($arrOption['label']), implode('', $arrOptgroups));
}
}
This code has been manually tested on my simple case, I don't know if it is "robust" enough to handle other cases.
How did you define in the CSS Groups widget that it is an opt group?
Sorry for the late answer, but you reply made me realize the backend was indeed not ready for managing optgroup.
I did create a GoupKeyValueWizard
widget (aka a KeyValueWizard
with a field dedicated to the group) and used it on the cssClasses
field, modified a bit the prepareData
function in tl_style_manager
and added the saveData
function as the save_callback
for the cssClasses
field.
You can find details here : https://github.com/Web-Ex-Machina/contao-component-style-manager/pull/2/files
Feel free to repoen it when you have a new approach