16

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?

29

Try this:

<depends>
    <field separator="|">
        <value>1|2|3</value>
    </field>
</depends>
  • Do you know that this works, or are you just putting it out there? – benmarks Feb 17 '14 at 0:04
  • 1
    Dude...I don't know how, but this actually works. Now I'm ashamed and sorry for all my bad words I said to the guys that implemented the config section. For some reason $dependent['separator'] returns the value of the attribute separator. To whom ever downvoted this, please upvote (twice if possible). Works perfectly. Thanks. cc @benmarks – Marius Feb 17 '14 at 7:16
  • 1
    This working != it being a good idea. I think there's no need to apologize for your words :-D – benmarks Feb 17 '14 at 13:27
7

[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.

  • 2
    Damn it. I know this kind of bugs :-) I hope you reported it? Great insight! – Fabian Blechschmidt Oct 24 '13 at 17:39
  • 1
    Oh man that is some great investigation are you sure you are not Sherlock? +1 for reporting it. – David Manners Oct 24 '13 at 20:07
  • 2
    @DavidManners elementary my dear watson – Marius Oct 24 '13 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.