Magento 2 - Two dependencies in one field form?
-
29-09-2020 - |
سؤال
It is possible to add 2 depedencies to 1 field?
Like this?
->addFieldMap($field_to_evaluate->getHtmlId(), $field_to_evaluate->getName())
->addFieldMap($field_to_show->getHtmlId(), $field_to_show->getName())
->addFieldDependence
(
$field_to_show->getName(),
$field_to_evaluate->getName(),
'dependecy 1'
)
->addFieldDependence
(
$field_to_show->getName(),
$field_to_evaluate->getName(),
'dependecy 2'
)
With this code it looks like only add the first dependency
Can i also indicate if the $field_to_show hides insteand of show?
المحلول
Edit
This seem to be an issue in Magento2, one possible workaround is
In your construct inject FieldFactory and also create a protected var for _fieldFactory
public function __construct(
......
\Magento\Config\Model\Config\Structure\Element\Dependency\FieldFactory $fieldFactory,
......
) {
....
$this->_fieldFactory = $fieldFactory;
....
}
Then update your existing code to
$refField = $this->_fieldFactory->create(
['fieldData' => ['value' => 'dependecy 1,dependecy 2', 'separator' => ','], 'fieldPrefix' => '']
);
->addFieldDependence
(
$field_to_show->getName(),
$field_to_evaluate->getName(),
$refField
)
I created an issue for this at https://github.com/magento/magento2/issues/4796
Try
->addFieldMap($field_to_evaluate->getHtmlId(), $field_to_evaluate->getName())
->addFieldMap($field_to_show->getHtmlId(), $field_to_show->getName())
->addFieldDependence
(
$field_to_show->getName(),
$field_to_evaluate->getName(),
array('dependecy 1','dependecy 2')
)
To hide the field by default, you should set the default selected value of that option to the option that would trigger it to hide (to prevent any UI confusion)
نصائح أخرى
$refField = $this->_fieldFactory->create(['fieldData' => ['value' => '1,2', 'separator' => ','], 'fieldPrefix' => '']
);
$this->setChild('form_after', $this->getLayout()->createBlock(
'Magento\Backend\Block\Widget\Form\Element\Dependence')
->addFieldMap($action->getHtmlId(), $action->getName())
->addFieldMap($anotherFieldweb->getHtmlId(), $anotherFieldweb->getName())
->addFieldMap($anotherFieldstore->getHtmlId(), $anotherFieldstore->getName())
->addFieldMap($anotherFieldhideorders->getHtmlId(), $anotherFieldhideorders->getName())
->addFieldMap($anotherFieldhideinvoice->getHtmlId(), $anotherFieldhideinvoice->getName())
->addFieldMap($anotherFieldhideshipment->getHtmlId(), $anotherFieldhideshipment->getName())
->addFieldMap($anotherFieldhidecredit->getHtmlId(), $anotherFieldhidecredit->getName())
->addFieldDependence($anotherFieldweb->getName(),$action->getName(),1)
->addFieldDependence($anotherFieldstore->getName(),$action->getName(),2)
->addFieldDependence($anotherFieldhideorders->getName(),$action->getName(),$refField)
->addFieldDependence($anotherFieldhideinvoice->getName(),$action->getName(),$refField)
->addFieldDependence($anotherFieldhideshipment->getName(),$action->getName(),$refField)
->addFieldDependence($anotherFieldhidecredit->getName(),$action->getName(),$refField)
);
لا تنتمي إلى magento.stackexchange