3

I'm using magento 2.2.4. In system configuration, I want to do apply dependency.

But, both groups and fields are different. It's working in same group. But, I can't get output for different group.

How to do it ? Please help me.

I added my system.xml file code :

system.xml :

<?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="sectionID" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Name</label>
            <tab>TabName</tab>
            <resource>CompanyName_ModuleName::methodName</resource>
            <group id="groupID" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Configuration</label>
                <field id="fieldID" 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="groupID2" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Form Configuration</label>
                <field id="fieldID2" 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>
                    <depends>
                        <field id="fieldID">1</field>
                    </depends>
                </field>
            </group>
        </section>
    </system>
</config>
1

It's only working if your field ID is under parent group ID. For apply dependency in other group, You need to add like 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="sectionID" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Name</label>
            <tab>TabName</tab>
            <resource>CompanyName_ModuleName::methodName</resource>
            <group id="groupID" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Configuration</label>
                <field id="fieldID" 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="groupID2" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Form Configuration</label>
                <field id="fieldID2" 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>
                    <depends>
                        <field id="sectionID/groupID/fieldID">1</field>
                    </depends>
                </field>
            </group>
        </section>
    </system>
</config>

Hope, It may be helpful for you. It should be working.

  • Happy Coding !! Happy to help :) – Rohan Hapani Aug 3 '18 at 17:28
2

The issue can be solved simply by mentioning the full path if the field is not in the current group. e.g.

<depends>
    <field id="section/group/field">1</field>
</depends>

Good Luck!

  • if used for group, it hides both groups. – Abid Malik Apr 20 at 8:41
0

if it is a same group, you can use wildcard for field id as

<depends>
    <field id="*/*/[depends_on_field_id]">someValue</field>
</depends>

if it is a same section, you also allowed to use wildcard as

<depends>
    <field id="*/[other_group_id]/[depends_on_field_id]">someValue</field>
</depends>

so this could be enough in your case

<depends>
    <field id="*/groupID/fieldID">1</field>
</depends>

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.