Magento2 system.xml配置depends的使用

Posted by

1.简单依赖上一个字段的值

意思是title字段是否显示依赖active字段的值是否等于1显示,等于0不显示。

<?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="payment">
            <group id="creditpay" translate="label comment" type="text" sortOrder="310" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Creditpay Cwalletco</label>
                <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="title" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Title</label>
                    <depends>
                        <field id="active">1</field>
                    </depends>
                </field>
 </group>
           
        </section>
    </system>
</config>

2. 多个字段的依赖

多个字段的依赖,按照依赖的先后顺序写:

display_payment_multi_type 字段依赖于active,display_payment_type

<?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="payment">
            <group id="creditpay" translate="label comment" type="text" sortOrder="310" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Creditpay Cwalletco</label>
                <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="display_payment_type" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Display Payment Type</label>
                    <source_model>Cg\CreditPayment\Model\Config\Custom</source_model>
                    <depends>
                        <field id ="active">1</field>
                    </depends>
                </field>
                <field id="display_payment_multi_type" translate="label" type="multiselect" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Display Payment Type</label>
                    <source_model>Cg\CreditPayment\Model\Config\Multiselect</source_model>
                    <depends>
                        <field id="active">1</field>
                        <field id="display_payment_type">1</field>
                    </depends>
                </field>
 </group>
           
        </section>
    </system>
</config>

3. 复杂依赖:一个字段依赖于另外一个group的值

假设我有这样一个group:tracking

<?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="cg_tracking" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Tracking</label>
            <tab>sendorder</tab>
            <resource>xxxx</resource>
            <group id="tracking" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Tracking Configuration</label>
                <field id="enable" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Tracking Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
 </group> 
        </section>
    </system>
</config>

同时我还有这样一个group

   <group id = "tmoki" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>xxxxx</label>
                <field id="tmoki_active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <depends>
                        <field id="cg_tracking/tracking/enable">1</field>
                    </depends>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="advertiser_id" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Advertiser ID</label>
                    <validate>required-entry</validate>
                    <depends>
                        <field id="cg_tracking/tracking/enable">1</field>
                        <field id="tmoki_active">1</field>
                    </depends>
                </field>
</group> 
        </section>
    </system>
</config>

假设我的tmoki组依赖于tracking组的“enable”,即

  <depends>
       <field id="cg_tracking/tracking/enable">1</field>
  </depends>

那么增加依赖关系后的最后结果应该是这样的

   <group id = "tmoki" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Tmoki.com Affiliate Tracking Configuration</label>
                <field id="tmoki_active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <depends>
                        <field id="cg_tracking/tracking/enable">1</field>
                    </depends>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="advertiser_id" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Advertiser ID</label>
                    <validate>required-entry</validate>
                    <depends>
                        <field id="cg_tracking/tracking/enable">1</field>
                        <field id="tmoki_active">1</field>
                    </depends>
                </field>
</group> 
        </section>
    </system>
</config>
depends