3

I know that to create a dependency for fields it's possible to use the node

<depends />

That said, I've been trying several ways to create a group dependency but I can't seem to find a way to make Magento use it, on the actual Form.php code it mentions

$dependent->fieldset

So it might be there but I haven't been able to leverage it.

Edit: On further investigation it seems it only loops through fields to read dependencies since on Mage_Adminhtml_Block_System_Config_Form::initFields()

foreach ($group->fields as $elements) {

It would still be a good thing to know how to achieve this

2
  • Hi, if you find any solution for your question,then feel free to answer your own question and accept it. If the below answer helped in your doubt, then please accept it. Please dont leave a question as it is. When you accept a answer it will help lot of future visitors Commented Jan 23, 2015 at 8:22
  • I'm still waiting for a solution, I haven't touched this issue myself
    – changeling
    Commented Jan 23, 2015 at 16:37

3 Answers 3

5

You can put JavaScript in the comment area. It can be something like this:

<comment><![CDATA[
    <script type="text/javascript">
        document.observe("dom:loaded",function(){
            if($('sections_groups_value').value != some_value){
                 Element.up($('sections_groups')).hide();
            }
        });
        Event.observe('sections_groups_value', 'change', function(){
            if(this.value != some_value){
                Element.up($('sections_groups')).hide();
            }else if(this.value == some_value){
                Element.up($('sections_groups')).show();
            }
        })
    </script>]]>
</comment>

Replace "sections_groups_value", "some_value" and "sections_groups" accordingly.

3

First of all, I need clarification on your below wording.

I've been trying several ways to create a group dependency

Are you really trying to achieve group dependency of field dependency ?

To make the point more clearer, groups and fields are different in system configuration. Groups means a collection of related fields.

A collection of related fields constitute a group. A collection of related groups constitute a section and collection of such sections constitute the entire system configuration page.

If you are talking about field dependency, then this link is good starting point.

If you really need to see a real time example, you can look into Mage_Customer module. There inside app/code/core/Mage/Customer/etc/system.xml, you can find definition for the group create_account. You can see that more than one fields in that group depends upon a field with name auto_group_assign. If you follow the same convention to declare your custom fields, then you can easily achieve what you really want.

Disclaimer :- Following section is based on my observation and it may or may not be correct. If I am wrong, please correct me :-)

From your edit, I can see that you are in the right direction.Hence I would like to give a small explanation on this part.

The method that you have referenced is Mage_Adminhtml_Block_System_Config_Form::initFields(). Mage_Adminhtml_Block_System_Config_Form is a class which is used to constitute system configuration form. initFields() is a method which is used to generate fields in a particular group.

If you closely look into this method, you can find three loops. Below I am showing their structure

foreach ($group->fields as $elements) {

    //doing sorting on fields in a specific group and 
    //store sorted array of fields in $elements.

    foreach ($elements as $element) {

        //more more codes

        if ($element->depends) {
            foreach ($element->depends->children() as $dependent) {
            }
        }
    }
}

Here first foreach loop collects fields that is coming inside a particular group (or fieldset). This collection is generated from xml configuration that is made through system.xml file. So field collection that $group->fields holds an unsorted array. Hence initField() method doing a sorting then and keep the sorted fields as an array in $elements. So the second foreach loop is again looping through a sorted array of fields.

A lot of things happen inside this loop. I am not going to explain what exactly happen there. However our area of interest is next foreach loop section. Before this loop executes, initFields() method checks for dependency of a field if ($element->depends). So dependency is specified, it iterates through those depending fields. ($element->depends->children() actually generates an array of depending fields. that is fields on which current field is depending on).

Wait... it iterating through a collection of depending fields !!!! It means that, we can have multiple number of field dependency for a particular field. At this moment I need to specify out that I am referencing to Magento 1.9.1. This feature is not available in older versions I think. (from magento 1.7, multiple field dependency is possible, I think).

And that's it. This is how field dependency is processing in magento. You will get more details on the above specified link. I just made the point clear.

2
  • I mean groups, i know how to make fields dependent, it's the first line "I know that to create a dependency for fields it's possible to use the node" i want to create a group that only appears if I select some value for a field in another group.
    – changeling
    Commented Jan 21, 2015 at 17:36
  • AFAIK, group dependency is not possible. Magento implemented field dependency through its code as you can see. The code that you were referencing through your question is actually points to field dependency implementation section and I could not find any relevant codes that will prove group dependency. Commented Jan 22, 2015 at 0:47
0

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.

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.