Question

I'm creating my own template and I need to set multiple default values for a groupedlist type form field.

What i did:

<field name="grid-top" 
       type="groupedlist" 
       label="COM_TPL_MYTEMPLATE_DIV_TYPE_LABEL" 
       description="COM_TPL_MYTEMPLATE_DIV_TYPE_DESC"
       multiple="true" 
       class="big"
       default="VALUE-1, VALUE-2" />

however this way doesnt work. anyone please point me to a right direction, How can i set multiple default values ?

Was it helpful?

Solution

The default value in the xml should be a JSON Encoded array.

So your XML should show as this:

<field name="grid-top" 
       type="groupedlist" 
       label="COM_TPL_MYTEMPLATE_DIV_TYPE_LABEL" 
       description="COM_TPL_MYTEMPLATE_DIV_TYPE_DESC"
       multiple="true" 
       class="big"
       default='["VALUE-1","VALUE-2"]' />

Also note that values must be protected by a double quote so you need to use a single quote to encalsulate the whole default value. Using default="['VALUE-1','VALUE-2']" would not work.

OTHER TIPS

To use the GroupedList you build it as you would a standard list you just wrap the options for each group in it's own <group /> element. You can also mix group's and stand-alone options in the same list menu.

e.g this example shows groups and options, you can of course use JText keys rather than straight text as I used in the example.

   <field name="experimentalGroupedList"
           type="groupedlist"
           label="Grouped List Experiment"
           default="0" >
        <option value="">Standard Option</option>
        <group label="Group #1">
            <option value="1">JYES</option>
            <option value="0">JNO</option>
        </group>
        <group label="Group #2 — Amphibians">
            <option value="frog">Frog</option>
            <option value="caecilian">Caecilian</option>
            <option value="salamander">Salamander</option>
        </group>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </field>

Field type list doesn't provide any option to set two or more default values. You can do it by using the singular model.

Find the singular model and find the function like public function getItem($pk = null) (if not exists then create it)

The function would be looked like this -

public function getItem($pk = null) {
    if ($item = parent::getItem($pk)) {
        if (empty($item->experimentalGroupedList)) {
            $item->experimentalGroupedList = array('VALUE-1', 'VALUE-2');
        }
        return $item;
    }
    return parent::getItem($pk);
}

This will set the default values for a multiple list field when no saved value found. Hope this will solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top