phpdrupaldrupal-7drupal-fieldshook-form-alter

How to programmatically set a conditional field with OR functionality in Drupal 7


In mymodule_form_alter() I am programmatically setting states for my conditional fields. I.E.

$form['my_field_one']['#states'] = array(
              'visible' => array(
                ':input[name="my_dependee_field[und]"]' => array('value' => 'primary'),
              ),
            );

And this works great!

However, I need to assign some of my fields the OR functionality (multiple allowed dependee values) and can't quite figure out how to assign it programmatically (please excuse my limited experience with PHP).

I've tried some things such as...

$form['my_field_one']['#states'] = array(
  'visible' => array(
    ':input[name="my_dependee_field[und]"]' => array('value' => array('0' => 'primary','1' => 'secondary',),),
  ),
);

...with (obviously) no luck.

Can any kind soul lend a helping hand, or point me in the right direction. It is crucial that I accomplish this programmatically.

Any assistance is GREATLY appreciated.

THANKS!


Solution

  • Update your #state using an array with two possible values:

    $form['my_field_one']['#states'] = array(
        'visible' => array(
            ':input[name="my_dependee_field[und]"]' => array(
                array('value' => 'primary'),
                array('value' => 'secondary'),
            ),
        ),
    );