Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
So, the ability to select is/is not empty for dropdown conditionals seems quite handy. I'm currently adding it in a custom module, but thought I would contribute the change.
This almost works out of the box just by modifying _webform_conditional_operator_info
, no more code required. However, in webform.js Drupal.webform.conditionalOperatorStringNotEmpty
was not registering the select components as empty, so I changed it to be an exact inverse of the sister conditionalOperatorStringEmpty function.
This is not extensively tested, but it has worked nicely on my development server so far.
Comment | File | Size | Author |
---|---|---|---|
#5 | webform_conditional_empty_select-2076409.patch | 2.2 KB | quicksketch |
stringNotEmptyConditional-1.patch | 2.03 KB | threewestwinds | |
Comments
Comment #1
quicksketchHi @threewestwinds, thanks for the patch! This looks like a great patch. I think this wasn't included originally because you can accomplish this already by specifying individual options. However, I see the usefulness of just having a global empty/not empty without needing to specify anything, especially with something like checkboxes.
Comment #2
threewestwinds CreditAttribution: threewestwinds commentedOur forms were rather difficult to set up before I patched to our site, since the options list changed regularly (I'm using hook_webform_select_options_info to pull in terms from a taxonomy vocabulary). There was a lot of manual and error prone work.
Even if the "is / is not empty" part for select lists lives in a different module, I don't see a downside to reducing duplication with the .js change.
Comment #3
quicksketchYeah I like the new JS shortcut. No idea why I didn't just do that in the first place. I'll need to double-check if it causes any weirdness.
I think the empty/not empty feature is fine to include. It's probably better to have this option for the convenience over the small amount of confusion this might create when the option exists for something that doesn't quite make sense (like a set of radio buttons that would always have a value).
Comment #4
quicksketchI finally reviewed this patch and it almost works, but using "is not empty" on a set of checkboxes doesn't work as expected. Apparently, checkboxes are always considered to be "not empty". I'll see if I can work this out.
Comment #5
quicksketchHere we go, all that was needed was to check that there was actually a value to loop over at all. If the returned value is an empty array, that now counts as empty. Previously the array had to contain an empty string to match.
Thanks for this patch @threewestwinds! Sorry it took forever to review and commit it.
Comment #7
quicksketchMarked #1638068: Add Additional Conditionals for Select Options duplicate, which has similar recommendations.
Comment #8
fenstratNeeds porting to 8.x-4.x.
Comment #9
fenstratCommitted and pushed 5fd72fe to 8.x-4.x. Thanks!