1

basically i want to show configuration fields only if value is set. since other fields require first value in order to call external source.

<field id="database" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="2" translate="label" type="select">
    <label>Database</label>
    <comment>choose database</comment>
    <source_model>Wemessage\ModuleX\Model\Config\Source\Database\Source</source_model>
    <frontend_model>Wemessage\ModuleX\Model\Config\Source\Database\Model</frontend_model>   
</field>

if i remove frontend_model i do see my field in configuration, if i add it back the configuration page is empty.

this is my frontend model file:

namespace Wemessage\ModuleX\Model\Config\Source\Database\Model;

use Wemessage\ModuleX\Helper\Data;

class Model extends \Magento\Config\Block\System\Config\Form\Field 
{
    /**
     * Render fieldset html
     *
     * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
     * @return string
     */
    public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
    {
        $html = '';
        //$helper = new Data();
        //$token = $helper->getConfigValue('module/api/token');
        if($token){
            $html .= parent::render($element);
        }
        return $html;
    }
}

any idea what can be wrong here ?

1 Answer 1

1

After trial and error solved my problem:

public function render(AbstractElement $element)
{
    $html = '';
    $token = $this->dataHelper->getConfigValue('module/api/token');
    if($token){
        return parent::render($element);
    } else {
        return $this->_decorateRowHtml($element, $html);
    }
}

public function _decorateRowHtml($element, $html)
{
    return '<tr id="row_' . $element->getHtmlId() . '">' . $html . '</tr>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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