1

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);
    }
}
1

This is what I finally did. If someone knows a cleaner solution please let me know.

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);
        $results = $db->loadObjectList();    

        $options = array();
        foreach ($results as $result) {

            // here you can do some logic that creates an array containing fieldx dependencies
            $fieldx_logic = array(1,2); // just an example

            $option =  JHTML::_('select.option', $result->value, Text::_($result->text));
            $attr = array('field' => 'jform[fieldx]', 'values' => implode(",", $fieldx_logic), 'sign' => '=', 'op' => '');
            $option->optionattr = "data-showon='[" . json_encode($attr) . "]'";
            $options[] = $option;
        }

        return array_merge(parent::getOptions(), $options);
    }
}
2
  • So you are adding this dynamic showon to every option in the list? The options are all unconditionally receiving the same showon rule?
    – mickmackusa
    Oct 7 '20 at 14:41
  • no. there is logic that handles which rules apply on which field, but that logic is trivial and not part of the question. read my comment in the code.
    – Mike
    Oct 7 '20 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.