When creating a showon attribute in list options one can do that in form XML since Joomla 3.9 like this:
<field name="fieldx" type="list" default="0" label="MYFIELDX">
<option value="0">0</option>
<option value="1">1</option>
</field>
<field name="myfield" type="list" default="0" label="MYFIELD">
<option value="0">A</option>
<option value="1">B</option>
<option value="2" showon="fieldx:1">C</option>
<option value="3">D</option>
</field>
I want to do that in a small custom field type I'm using in my component. Question is how can i add the showon attribute to options dynamically? I checked the pull request it was introduced by with no luck: https://github.com/joomla/joomla-cms/pull/18998
defined('JPATH_BASE') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Form\FormHelper;
FormHelper::loadFieldClass('list');
class JFormFieldObjecttypes extends JFormFieldList
{
protected $type = 'Objecttypes';
public function getOptions()
{
$options = array();
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->select('id AS value, title AS text');
$query->from('#__mytable');
$query->where('published = 1');
$query->order('ordering');
$db->setQuery($query);
$options = $db->loadObjectList();
foreach ($options as $option) {
$option->text = Text::_($option->text);
$option->showon = 'fieldx:1'; // stupid ofc
}
return array_merge(parent::getOptions(), $options);
}
}