magentomagento-1.4

can a magento adminhtml field depend on more then one field or value?


In http://alanstorm.com/magento_system_configuration_in_depth_tutorial @AlanStorm gives a very good tutorial for system configuration.

He also explains how to use a <depends> tag to make a field show only when a specific value is set in another field.

My Q is how can I make fieldB visible if field A has either value V1 or V2. and are there any other options with the <depends> ?

Also If someone knows where in magento's code this is implemented I would also like to have a look at the code myself.

Thanks


Solution

  • There is a better way, but you will need to override the core files

    Override the method under the following file app\code\core\Mage\Adminhtml\Block\Widget\Form\Element\Dependence.php

        public function addFieldDependence($fieldName, $fieldNameFrom, $refValues)
    {
        /*
        if (is_array($refValues)) {
            Mage::throwException('Dependency from multiple values is not implemented yet. Please fix to your widget.xml');
        }
        */
        $this->_depends[$fieldName][$fieldNameFrom] = $refValues;
        return $this;
    }
    

    On the app\code\core\Mage\Adminhtml\Block\System\Config\Form.php Modify the method initFields

    if ($e->depends) {
                    foreach ($e->depends->children() as $dependent) {
                        $dependentId = $section->getName() . '_' . $group->getName() . '_' . $fieldPrefix . $dependent->getName();
                        if ($dependent->count()) {
                            $dependentValue = (array) $dependent;
                            $dependentValue = array_values($dependentValue);
                        } else {
                            $dependentValue = (string) $dependent;
                        }
    
                        $this->_getDependence()
                            ->addFieldMap($id, $id)
                            ->addFieldMap($dependentId, $dependentId)
                            ->addFieldDependence($id, $dependentId, $dependentValue);
                    }
                }
    

    Modify the javascript file js\mage\adminhtml\form.js

    trackChange : function(e, idTo, valuesFrom)
    {
        // define whether the target should show up
        var shouldShowUp = true;
        for (var idFrom in valuesFrom) {
    
            if (valuesFrom.hasOwnProperty(idFrom)) {
                if (typeof(valuesFrom[idFrom])=="object") {
                    shouldShowUp = false;
                    for(var idVal in valuesFrom[idFrom]) {
                        if (valuesFrom[idFrom].hasOwnProperty(idVal)) {
                            if (typeof(idVal)!="undefined" && ($(idFrom).value == valuesFrom[idFrom][idVal])) {
                                shouldShowUp = true;
                            }
                        }
                    }
                } else if (typeof(valuesFrom[idFrom])=="string") {
                    if ($(idFrom).value != valuesFrom[idFrom]) {
                        shouldShowUp = false;
                    }
                }
            }
            /*
            if ($(idFrom).value != valuesFrom[idFrom]) {
                shouldShowUp = false;
            }
            */
        }
    
        // toggle target row
        if (shouldShowUp) {
            $(idTo).up(this._config.levels_up).select('input', 'select', 'td').each(function (item) {
                if (!item.type || item.type != 'hidden') { // don't touch hidden inputs, bc they may have custom logic
                    item.disabled = false;
                }
            });
            $(idTo).up(this._config.levels_up).show();
        } else {
            $(idTo).up(this._config.levels_up).select('input', 'select', 'td').each(function (item){
                if (!item.type || item.type != 'hidden') { // don't touch hidden inputs, bc they may have custom logic
                    item.disabled = true;
                }
            });
            $(idTo).up(this._config.levels_up).hide();
        }
    }
    

    Use the following syntax for multiple values dependency on your xml

                                <depends>
                                <field1>
                                    <val1>text</val1>
                                    <val2>radio</val2>
                                </field1>
                            </depends>