FAPI #states: Fix conditionals to allow OR and XOR constructions

Created on 8 March 2010, over 14 years ago
Updated 25 October 2023, about 1 year ago

The version that went in with #557272: FAPI: JavaScript States system β†’ unfortunately was a bit premature and didn’t include support for OR: it was only possible to write AND conditions. This patch fixes this behavior while maintaining full backwards compatibility (all valid existing states descriptions work in exactly the same way with this patch).

Instead of just ANDing all conditions together, this patch makes states.js aware of arrays. All elements in an object literal ({ ... }) are ANDed (like now) and all elements in a array literal ([ ... ]) are ORed. This allows you to write conditions like:

"enabled": [
  {"select[name=\"date_default_timezone\"]": { "value": "America/Winnipeg" }},
  {"select[name=\"date_first_day\"]": [{ "value": "2" },{ "value": "3" }]}
]

In PHP, this would be:

array(
  "enabled" => array(
    array("select[name=\"date_default_timezone\"]" => array("value" => "America/Vancouver" )),
    array("select[name=\"date_first_day\"]" => array(array("value" => "2"), array("value" => "3"))),
  )
)

Note that it’s possible to use the array notation (without =>, thus with numeric indexes in PHP) both around selectors and within selectors. In the above example, the element is enabled when either the default timezone is Vancouver or when the user selects Tuesday or Wednesday as week start days (full sample code attached).

The XOR operator is supported as well: when writing an OR statement, you can insert the string 'xor' at any position to make the OR exclusive:

array(
  "enabled" => array('xor',
    array("select[name=\"date_default_timezone\"]" => array("value" => "America/Vancouver" )),
    array("select[name=\"date_first_day\"]" => array(array("value" => "2"), array("value" => "3"))),
  )
)

is possible, but

array(
  "enabled" => array(
    array("select[name=\"date_default_timezone\"]" => array("value" => "America/Vancouver" )),
    'xor',
    array("select[name=\"date_first_day\"]" => array(array("value" => "2"), array("value" => "3"))),
  )
)

works just as well.

πŸ› Bug report
Status

Fixed

Version

7.0 ⚰️

Component
JavascriptΒ  β†’

Last updated 1 day ago

Created by

πŸ‡©πŸ‡ͺGermany kkaefer

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024