system.xml field dependency
-
16-10-2019 - |
Question
So you can set in the system.xml
file of a module a dependency between fields by adding a <depends>
tag in one of the fields.
<field1>
....
</field1>
<field2>
....
<depends>
<field1>1</field1>
</depends>
</field2>
The code above means that field2
will be shown when the value of field1
is 1
.
I want to know how/if can I tell Magento to show field2
if the value for field1
is 1
OR 2
?
Solution
Try this:
<depends>
<field separator="|">
<value>1|2|3</value>
</field>
</depends>
OTHER TIPS
[EDIT]
I was wrong in my answer below. I will not delete it (yet) because I got 7 upvotes on this :). But I'm editing it so you all have the chance to retract your vote (even downvote it, because I deserve it).
Original Answer
Ha!..I found it.
Short answer: You cannot!
Long answer: You should be able to do it if someone would have known the difference between an array and an object.
In theory this should work
<field1>
....
</field1>
<field2>
....
<depends>
<field1>
<value>1|2</value>
<separator>|</separator>
</field1>
</depends>
</field2>
But in the code that handles the dependency, Mage_Adminhtml_Block_System_Config_Form::initFields
around line 366 there is this code
if (isset($dependent['separator'])) {
$dependentValue = explode((string)$dependent['separator'], $dependentValue);
}
$dependent
is always an object so $dependent['separator']
is never set.
If I change the code above to
if (isset($dependent->separator)) {
$dependentValue = explode((string)$dependent->separator, $dependentValue);
}
everything works smoothly.
I guess I cannot change the core just for the sake of an extension so I have to create 2 fields instead of 1, one for each value from field1
or create a custom js that handles this and add it to the config page.