1

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?

asked May 30, 2016 at 14:41

2 Answers 2

2

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)

answered May 30, 2016 at 18:29
2
  • I tried that, did not work get error Notice: Array to string conversion taking a look at \vendor\magento\module-backend\Block\Widget\Form\Element\Dependence.php the first dependency gets overwritten by the seconed if a separe it $this->_depends[$fieldName][$fieldNameFrom] = $refField; Commented May 31, 2016 at 6:57
  • Wow ty for your interest, have not tried it yet but it seems it may work Commented Jun 13, 2016 at 6:49
-1
$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)
 );
Abhishek Tripathi
2,9152 gold badges21 silver badges38 bronze badges
answered Apr 4, 2019 at 6:40
1
  • This is perfect solution Commented Apr 4, 2019 at 6:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.