Can a magento adminhtml field depend on more than one field or value?

The http://alanstorm.com/magento_system_configuration_in_depth_tutorial @AlanStorm gives a very good guide on how to configure the system.

It also explains how to use the <depend> tag so that a field is displayed only when a specific value is specified in another field.

My Q - how can I make a visible field if field A has either V1 or V2. and are there any other options with the <depend> parameter?

Also, if someone knows where in the magenta code this is implemented, I would also like to see the code myself.

thanks

+5
magento
source share
4 answers

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

Override the method in the following file Application \ 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; } 

In the application \ code \ core \ Mage \ Adminhtml \ Block \ System \ Config \ Form.php Change the initFields method

 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 javascript js \ mage \ adminhtml \ form.js file

 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 to depend on multiple values ​​on your xml

  <depends> <field1> <val1>text</val1> <val2>radio</val2> </field1> </depends> 
+3
source share

If I understand correctly the release notes from Magento 1.7.0.1, this functional function has been implemented (http://goo.gl/ZgHG0). I have successfully tested it on Magento CE 1.7.0.2.

You must declare the delimiter parameter depending on the field as follows:

 <depends> <depends_from_field separator=","> depends_from_field_value_1,depends_from_field_value_2 </depends_from_field> </depends> 

Please note that depends_from_field_value_1 and depends_from_field_value_2 are separated by a comma, the exact character declared in separator=","

+13
source share

I'm not sure where Alan's article explains this, but there is how I do it: this is just a bit of javascript.
In your group you put a comment tag with embedded javascript.
For example, here is my code that checks the value of one field to show (or not) another:

 <?xml version="1.0" encoding="UTF-8"?> <config> <sections> <points_options translate="label" module="points"> <tab>general</tab> <label>Loyalty Points</label> <frontend_type>text</frontend_type> <sort_order>1002</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <groups> <config_points translate="label"> <label>Configuration</label> <comment><![CDATA[ <script type="text/javascript"> checkExpirationPeriod = function() { if ($('points_options_config_points_expiration_period').getValue() > 0) { $('points_options_config_points_expiration_reminder').up(1).appear(); } else { $('points_options_config_points_expiration_reminder').up(1).fade(); } } Event.observe(window, 'load', function() { Event.observe('points_options_config_points_expiration_period', 'change', checkExpirationPeriod); checkExpirationPeriod(); }) </script> ]]></comment> 

as you can see, I am writing a small function that checks one field value to determine whether the other should show or not. Then I bind the onchange event to the function and run the function to display the correct fields when the page loads.
For your needs, just add the condition to the js function.
Hope that helps

+3
source share

Andrew's answer almost did the trick. I am now on 1.6.2.0, and I changed the initFields() method in app\code\core\Mage\Adminhtml\Block\System\Config\Form.php as follows:

 if ($e->depends) { foreach ($e->depends->children() as $dependent) { Mage::log((array)$dependent); $dependentId = $section->getName() . '_' . $group->getName() . '_' . $fieldPrefix . $dependent->getName(); if ($dependent->hasChildren()) { $dependentValue = (array) $dependent; $dependentValue = array_values($dependentValue); } else { $dependentValue = (string) $dependent; } $shouldBeAddedDependence = true; $dependentFieldName = $fieldPrefix . $dependent->getName(); $dependentField = $group->fields->$dependentFieldName; /* * If dependent field can't be shown in current scope and real dependent config value * is not equal to preferred one, then hide dependence fields by adding dependence * based on not shown field (not rendered field) */ if (!$this->_canShowField($dependentField)) { $dependentFullPath = $section->getName() . '/' . $group->getName() . '/' . $fieldPrefix . $dependent->getName(); if (is_array($dependentValue)) { foreach ($dependentValue as $dependentOption) { $shouldBeAddedDependence |= $dependentOption != Mage::getStoreConfig( $dependentFullPath, $this->getStoreCode() ); } } else { $shouldBeAddedDependence = $dependentValue != Mage::getStoreConfig( $dependentFullPath, $this->getStoreCode() ); } } if($shouldBeAddedDependence) { $this->_getDependence() ->addFieldMap($id, $id) ->addFieldMap($dependentId, $dependentId) ->addFieldDependence($id, $dependentId, $dependentValue); } } } 

Also, you do not need to edit the main files. I will redo the admin theme to insert my own version of form.js and rewrite the two PHP classes using the config.xml custom module.

0
source share

All Articles