to use it in your Data Container Array please add
'my_field' => array(
// ... field configuration entries
'inputType' => 'select',
// ... field configuration entries
);
to the field configuration as ‘select’ is the corresponding alias for the SelectMenu widget
It features data retrieving from the following sources:
- an array
- a foreign field from a custom table
- a custom callback
Using array as source
Define the options array<?php
$myOptions = array(
'a' => 'label a',
'b' => 'label b'
);
$GLOBALS['TL_DCA']['my_dca'] = array(
// other sections like palettes etc.
'fields' => array(
'my_field' => array(
'inputType' => 'select',
'options' => $myOptions
),
// ... other fields
)
);
?>
the resulting html will be:
<select name="wselectmenu" id="ctrl_wselectmenu" class="tl_select mandatory" onfocus="Backend.getScrollOffset();">
<option value="a">label a</option>
<option value="b">label b</option>
</select>
Grouping options
Options can be grouped by putting them as array element, starting from the code above:$myOptions = array(
'my_group' => array(
'a' => 'label a',
'b' => 'label b'
)
);
<select name="wselectmenu" id="ctrl_wselectmenu" class="tl_select mandatory" onfocus="Backend.getScrollOffset();">
<optgroup label=" my_group">
<option value="a">label a</option>
<option value="b">label b</option>
</optgroup>
</select>
Labelling the group
Labels can be managed through an array; e.g.:'my_field' => array(
// ... other field entries
'options' => $myOptions,
'reference' => $GLOBALS['TL_LANG']['tl_dev_sandbox']['optionsLabel']
// ... other field entries
);
this the method used by TYPOlight core extensions and it makes translations in other languages easy.
$GLOBALS['TL_LANG']['tl_dev_sandbox']['optionsLabel']= array(
'my_group' => 'label for my group',
);
<select name="wselectmenu" id="ctrl_wselectmenu" class="tl_select mandatory" onfocus="Backend.getScrollOffset();">
<optgroup label=" label for my group">
<option value="a">label a</option>
<option value="b">label b</option>
</optgroup>
</select>
Using the reference array to label the whole select items
Using the code above we manage the labels for options through the $myOptions array (‘a’ => ‘label a’) while the groups labels are managed through the reference array.Applying a small change we can manage all the select items (groups and options) with the reference array.
change the $myOptions array so it will looks like:
$myOptions = array(
'my_group' => array(
'a',
'b'
)
);
$GLOBALS['TL_LANG']['tl_dev_sandbox']['optionsLabel'] = array(
'my_group' => 'label for my group',
'a' => 'label for option a',
'b' => 'label for option b'
);
Querying the database to populate the options
This is useful when you have a foreignKey relationship with another table (e.g. the tl_article table refers to tl_user to associate its own author).To use this feature just specify the table and field from which you want to populate the selectbox
please note:e.g. the following field configuration entry:
table must have a field named id. For each row retrieved the id will be used as value and the field will be used as label.
'my_field' => array(
'inputType' => 'select',
'foreignKey' => 'tl_user.name',
)
Using custom callback to generate the options
Sometime you need more control on options. Luckily the options can be generated through a custom callback.Create the callback:
please note:
the standard way used by TYPOlight core extensions when dealing with callback is to create a class named as the dca and put it into the dca file. The callback class will extends Backend.
class tl_dev_sandbox extends Backend {
/***
* since Backend::__construct is protected we must
* redefine it as public
*/
public function __construct() {
parent::__construct();
}
public function generateOptions() {
// you can use the logic explained above to create groups
$myOptions = array();
// fill in the myOptions array
// this is useless but you can query the database or read data from somewhere
$myOptions = array('a', 'b');
return $myOptions;
}
}
'my_field' => array(
'inputType' => 'select',
'options_callback' => array('tl_dev_sandbox', 'generateOptions')
)
[...] more here: Select configuration: Manage options « development with TYPOlight … By admin | category: Object, TYPOlight | tags: also-been, callback-class, [...]
ReplyDeleteTHX helped a lot.
ReplyDelete