oveleon / contao-component-style-manager

This extension is for the Contao CMS and allows you to easily manage and provide custom categories and groups for CSS classes.
MIT License
23 stars 6 forks source link

Unable to create <optgroup> with current code #65

Closed ChipsVII closed 1 year ago

ChipsVII commented 2 years ago

Hello,

I am unable to create selects 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="&nbsp;%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.

doishub commented 2 years ago

How did you define in the CSS Groups widget that it is an opt group?

ChipsVII commented 2 years ago

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

zoglo commented 1 year ago

Feel free to repoen it when you have a new approach