I'm trying to pre-select several options in a custom form field. I created a new JFormField which extends JFormFieldList and gets its values from my database. This is my current code:
protected function getOptions()
{
$app = JFactory::getApplication();
$cid = $app->input->get('cid' , null);
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('cs.id, cs.name');
$query->from($db->quoteName('#__asdasd', 'cs'));
$db->setQuery((string) $query);
$results = $db->loadObjectList();
$options = array();
if ($results)
{
foreach ($results as $result)
{
$options[] = JHtmlSelect::Option($result->id, $result->name);
}
}
// Merge any additional options in the XML definition.
$options = array_merge(parent::getOptions(), $options);
return $options;
}
This works perfectly, however I can't figure out how to preselect some of the entries. Basically, I just want to add "selected=selected" to some of the options.
Any clue how I can get this to work? I already read the API Documentation, however I find it to be confusing (https://api.joomla.org/cms-3/classes/JHtmlSelect.html#method_option). Is $optKey
the Argument I'm looking for? If this is the case, what attributes do I have to pass in order for it to work?