15

How can you set <depends> for a field which is not in the same group of fields

<?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="section" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>Name</label>
 <tab>tabname</tab>
 <resource>Namespace_ModuleName::method</resource>
 <group id="group" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>General Configuration</label>
 <field id="field" 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 Configuration</label>
 <field id="disable_certificate_check" 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="field">1</field>
 </depends>
 </field>
 </group>
 </section>
 </system>
</config>
asked Nov 3, 2016 at 0:02
0

3 Answers 3

59

Field id from depends node must contain section, group and field id's of the field you want to depend

<depends>
 <field id="section_id/group_id/field_id">1</field>
</depends>
answered Nov 3, 2016 at 0:02
5
  • Yes , try same way , but when in select "No" from the first group then hide both groups, can you please elaborate your answer on more. Commented Jun 28, 2017 at 13:13
  • 1
    @St3phan, How can we hide a whole group? Commented Sep 19, 2017 at 9:49
  • @mshakeel, I do not know if it can but let me have a little time to test. Commented Sep 19, 2017 at 10:53
  • 1
    @St3phan, did it using Javascript (answered). Share if you find a proper way. Commented Sep 20, 2017 at 8:01
  • You can use your jQuery code, sure. Commented Sep 20, 2017 at 8:12
7
<?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>
 <tab id="your_id" translate="label" sortOrder="1000">
 <label>your_label</label>
 </tab>
 <section id="your_id" translate="label" type="text" sortOrder="340" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>your_label</label>
 <tab>your_tab</tab>
 <resource>Your_Module::config</resource>
 <group id="your_id" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>your_label</label>
 <field id="your_id" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>your_label</label>
 <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
 </field>
 <field id="your_id" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>your_label</label>
 <depends>
 <field id="*/*/active">1</field>
 </depends>
 </field>
 <field id="your_id" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>your_label</label>
 <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
 <depends>
 <field id="*/*/active">1</field>
 </depends>
 </field>
 </group>
 <group id="your_id" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>your_label</label>
 <field id="your_id" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>your_label</label>
 <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
 </field> 
 <depends>
 <field id="section_id/group_id/field_id">1</field>
 </depends>
 </group> 
 </section>
 </system>
</config>
Mukesh
1,4844 gold badges29 silver badges59 bronze badges
answered Nov 22, 2018 at 11:04
1

Hide Configuration Groups

For magento 2.1.x, you can use following jQuery to toggle dependent Configuration Groups:

<comment><![CDATA[
<script type="text/javascript">//<![CDATA[
 require(['jquery'], function(){
 if (jQuery('#field_id').val() == 'value_to_compare') {
 toggleDependantGroups(true);
 }
 jQuery('#field_id').change(function() {
 if (jQuery(this).val() == 'value_to_compare') {
 toggleDependantGroups(true);
 } else {
 toggleDependantGroups(false);
 }
 });
 function toggleDependantGroups(hide=true)
 {
 if (hide) {
 jQuery('#section-id').closest('div.section-config').hide();
 jQuery('#section-id').closest('div.section-config').hide();
 jQuery('#last-visible-section-id').closest('div.section-config').css('border-bottom-width', '0px');
 } else {
 jQuery('#section-id').closest('div.section-config').show();
 jQuery('#section-id').closest('div.section-config').show();
 jQuery('#last-visible-section-id').closest('div.section-config').css('border-bottom-width', '1px');
 }
 }
 });
</script>]]>

Replace ids where necessary.

answered Sep 20, 2017 at 7:59
2
  • where to write this script? in which file? Commented Sep 15, 2020 at 7:15
  • Yes, what js file supports a system.xml layout definition? Commented Mar 22, 2021 at 13:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.