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.