I am creating a custom field extending JFormFieldList as below
Form field is defined as below
<field name="reltype_rev_id"
multiple="true"
type="customfield"
default="0"
label="Custom Field"
/>
Options (data) for the field is formed as below
public function getOptions($id='') {
$options = [(object)['key' => '', 'value' => 'Custom Field']];
$val_1 = new stdClass();
$val_1->key='1';
$val_1->value='One';
$val_2 = new stdClass();
$val_2->key='2';
$val_2->value='Two';
$key_values = array($val_1, $val_2);
$options = array_merge($options, $key_values);
$default_values = array(1);
return JHtml::_('select.options', $options, 'key', 'value', $default_values);
}
How can we mark some of the options as selected while forming the field.
The output comes as below
Update:
Optimized the code based on @zollie and the result is the same as shown in the above screenshot
$data = array('1' => 'One', '2' => 'Two');
return JHtmlSelect::options($data, 'value' , 'text', array('1'), false);