0

Currently, using the below code in the system.xml

Does anyone have a more efficient way (using only one Field or some other way) to do this?

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="vendor" translate="label" type="text" sortOrder="500" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Name of Module</label>
            <tab>tabname</tab>
            <resource>Namespace_ModuleName::method</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General</label>
                <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
            <group id="connection" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Connection</label>
                <field id="ssl" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Check</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>                    
                </field>
                <field id="comment1" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>&nbsp;</label>
                    <comment>If Yes Selected from Field ssl this comment should show.</comment>
                    <depends>
                        <field id="vendor/connection/ssl">1</field>
                    </depends>
                </field>
                <field id="comment2" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>&nbsp;</label>
                    <comment>If No Selected from Field ssl this comment should show.</comment>
                    <depends>
                        <field id="vendor/connection/ssl">0</field>
                    </depends>
                </field>
            </group>
        </section>
    </system>
</config>

Using the above code(Separated Fields for each comment) seems to be working as different fields are involved.

1
  • 1
    I have done it , 5 mins please Commented Dec 21, 2019 at 11:29

1 Answer 1

2

Answer: Working Solution

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="vendor" translate="label" type="text" sortOrder="500" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Name of Module</label>
            <tab>tabname</tab>
            <resource>Namespace_ModuleName::method</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General</label>
                <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
            <group id="connection" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Connection</label>
                <field id="ssl" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Check</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>                    
                    <comment>
                        <![CDATA[
                            <script>
                                require(['jquery'], function($){
                                    addExtremblerComment();
                                    $('#vendor_general_active').change(function(){
                                        $('#vendor_general_connection').parent('td').children('p').remove();
                                        addExtremblerComment();
                                    });
                                    function addExtremblerComment()
                                    {
                                        if($('#vendor_general_active').val() == true){
                                            $('#vendor_general_connection').after('<p class="note">If Yes Selected from Field ssl this comment should show.</p>');
                                        }
                                        if($('#vendor_general_active').val() == false){
                                            $('#vendor_general_connection').after('<p class="note">If No Selected from Field ssl this comment should show.</p>');
                                        }
                                    }
                                });
                            </script>
                        ]]>
                    </comment>
                </field>
            </group>
        </section>
    </system>
</config>
1
  • Thank you for the answer
    – sandip
    Commented Dec 21, 2019 at 11:51

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.