Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By ryan.gibson on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
7.x, 8.x
Introduced in version:
7.14
Issue links:
Description:
Change
- #states now support OR and XOR conditions
Example
$form['dependent_1']['#states'] = array(
'disabled' => array(
// dependee_1 has value ON
'[name="dependee_1"]' => array('value' => 'ON'),
array(
// At least one of dependee_2 or dependee_3 has value ON
array('[name="dependee_2"]' => array('value' => 'ON')),
array('[name="dependee_3"]' => array('value' => 'ON')),
),
array(
// Only one of dependee_4 or dependee_5 can have value ON
array('[name="dependee_4"]' => array('value' => 'ON')),
'xor',
// The field should be disabled when Select #1 has value ON; at least one
// of Select #2 and Select #3 has value ON; one but only one of Select #4
// and Select #5 has value ON.
array('[name="dependee_5"]' => array('value' => 'ON')),
),
)
);
Impacts:
Site builders, administrators, editors
Module developers
Themers
Comments
I can't make this code working
I've followed your instructions but my code doesn't work. I have this situation:
two select boxes, if you select option '' and 3 on the first, the second disappers.
For this, i've written this code:
But when i change the first select, the second doesn't disappear.
Have you got any solutions? Maybe my code is wrong?
Thans,
C.
Simple, individual AND/OR/XOR examples
I have always used https://www.lullabot.com/articles/form-api-states as my go-to article for remembering/revisiting conditionals in #states. The code snippet in this change record is confusing, and demonstrates 3 different examples (simple condition, then an OR, and then an XOR, all wrapped in additional arrays, which basically puts an OR between the examples).
Here's a simplified version of the various conditionals allowed within states after this change:
Simple condition:
AND condition:
OR condition (same as the previous example, but conditions are wrapped within additional arrays):
XOR condition (same as the previous example, but there's a textual 'xor' between the OR arrays):