Thursday, October 29, 2009

Select configuration: Manage options

SelectMenu is a powerful widget that let user choose a value from a select box.
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'
)
);
will be rendered as:

<select name="wselectmenu" id="ctrl_wselectmenu" class="tl_select mandatory" onfocus="Backend.getScrollOffset();">
<optgroup label="&nbsp;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
);
the reference array can be put into languages/your_language_code/your_dca_name dir (e.g. languages/en/tl_dev_sandbox.php for english and tl_dev_sandbox table) so it will be loaded when your dca is loaded.

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',
);
the resulting html will be:

<select name="wselectmenu" id="ctrl_wselectmenu" class="tl_select mandatory" onfocus="Backend.getScrollOffset();">
<optgroup label="&nbsp;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'
)
);
then add the missing entries to the reference array

$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:
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.
e.g. the following field configuration entry:


'my_field' => array(

'inputType' => 'select',
'foreignKey' => 'tl_user.name',
)
will populate a select with id as values and name as labels querying the table tl_user.

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;
}
}
then change your field configuration:

'my_field' => array(
'inputType' => 'select',
'options_callback' => array('tl_dev_sandbox', 'generateOptions')
)
That’s it. If you have questions please comment below.

2 comments:

  1. [...] more here:  Select configuration: Manage options « development with TYPOlight … By admin | category: Object, TYPOlight | tags: also-been, callback-class, [...]

    ReplyDelete