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?

1

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)

  • 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; – Gianni Di Falco May 31 '16 at 6:57
  • Wow ty for your interest, have not tried it yet but it seems it may work – Gianni Di Falco Jun 13 '16 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)
        );

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.