Magento 2: Поле залежить від system.xml, коли поля не в одній групі


10

Як можна встановити <depends>поле, яке не належить до однієї групи полів

<?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>

Відповіді:


40

Ідентифікатор поля від вузла залежить повинен містити розділи, групи та ідентифікатори поля, від якого ви хочете залежати

<depends>
    <field id="section_id/group_id/field_id">1</field>
</depends>

Так, спробуйте так само, але коли в першій групі виберіть "Ні", а потім прихойте обидві групи, ви можете, будь ласка, детальніше відповісти.
Друмін

1
@ St3phan, Як ми можемо приховати цілу групу?
mshakeel

@mshakeel , я не знаю, чи може це, але дай мені трохи часу на тестування.
St3phan

1
@ St3phan, зробив це за допомогою Javascript (відповів). Поділіться, якщо знайдете правильний шлях.
mshakeel

Ви можете використовувати код jQuery, звичайно.
St3phan

1

Сховати групи конфігурацій

Для magento 2.1.x ви можете використовувати наступні jQuery для переключення залежних груп конфігурації:

<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>]]>

Замініть ідентифікатори, де це необхідно.


1
<?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>
Використовуючи наш веб-сайт, ви визнаєте, що прочитали та зрозуміли наші Політику щодо файлів cookie та Політику конфіденційності.
Licensed under cc by-sa 3.0 with attribution required.