Problem/Motivation

Currently the #states API has some support for some level of nesting with conditionals. It would be great if the webform UI supported creating and editing these nested conditionals.

Proposed resolution

  • Investigate the current status of conditionals in the #states API.
  • Look at ways of representing this in the UI.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Sam152 created an issue. See original summary.

Sam152’s picture

We are working with some complex conditionals that require nesting. Editing the yaml has been a great way to get these going fast but it has required some level of technical expertise.

Similarly, I've found it really hard to understand the scope of what is supported in #states in terms of:

  • Logical operators (or/and) at which levels of depth (it looks like these are supported at the root level, but the second level is always an "and").
  • Depth in general, how depth is structured and the limitations (I believe only 1 level of depth is supported?).

We also have scenarios that require "or" conditions at the level 1 depth. If we take some conditions like:

A and (B or C)

We've managed to expand these conditions out into the following, in order to work within the constraints of what is offered by the states API:

(A and B) or (A or C)

I wonder if the webform UI could support both "and" conditions and "or" conditions at both levels of depth, and then use some technique of expansion to transform them into a set of conditions that states supports.

jrockowitz’s picture

The \Drupal\webform\WebformSubmissionConditionsValidator would also have to be updated to support nested conditions. This is a major undertaking which I personally don't have the resources or mental capacity to pursue.

We might consider copying the Views UI but I find Views nested filters confusing. The Conditional Fields module UI is using multi-step process and it is a little overwhelming.

I have to give @quicksketch credit for building a reasonable UI for complex use cases in the Webform module for D7.

I have a hard time wrapping my head around processing nested conditions and then building a sensible UI is even more challenging. A lot of form builders on the market have avoided supporting nested conditions.

Finally, I think Drupal core might need to update #states API and provide some sort of server-side validation to admin forms, especially if Form API starts supporting React base form.

A very reasonable MVP would be to update WebformSubmissionConditionsValidator to support nested conditions and continue to use YAML for writing advanced #states. There should be enough existing test coverage to prevent regressions while improving WebformSubmissionConditionsValidator.

Sam152’s picture

Thank you for the analysis!

Adding a related issue which was the sub-conditionals issue which introduced similar nested functionality to D7, might help provide some point of reference for the kind of effort it takes to add such a UI to an existing non-nested UI.

I agree that additions to the UI would need to be done in such a way that were easy to maintain and didn't significantly increase the complexity and maintenance burden of the existing UI.

The \Drupal\webform\WebformSubmissionConditionsValidator would also have to be updated to support nested conditions.

Good point, I've spun this off into #2985910: Extend support for nested conditionals in WebformSubmissionConditionsValidator.. I agree this would be a good step to take towards first class nested support, that would be decoupled from building any kind of UI.

jrockowitz’s picture

To begin to wrap my head around this issue I just wanted to post some YAML that illustrates a nested #states conditional (via a_and_b_or_c)

a:
  '#type': checkbox
  '#title': a
b:
  '#type': checkbox
  '#title': b
c:
  '#type': checkbox
  '#title': c
a_and_b:
  '#type': textfield
  '#title': '[a and b] = required'
  '#states':
    required:
      ':input[name="a"]':
        checked: true
      ':input[name="b"]':
        checked: true
a_and_b_or_c:
  '#type': textfield
  '#title': '[a and b] or c = required'
  '#states':
    required:
      -
        - ':input[name="a"]':
            checked: true
          ':input[name="b"]':
            checked: true
      - or
      - ':input[name="c"]':
          checked: true
jrockowitz’s picture

I really don't want to support nested conditionals via the UI because the UX becomes too complex. For an element's #states advanced users can edit the YAML source. The only problem is that a handler's conditional logic's YAML can't be edited.

I am tempted to add an Ajax button link to switch on the ability to edit a #states YAML source.

  • jrockowitz committed 8613e6b on 2984995-states-edit-source
    Issue #2984995 by jrockowitz: Expand the conditionals UI to support...

  • jrockowitz committed 5f8e74f on 2984995-states-edit-source
    Issue #2984995 by jrockowitz: Expand the conditionals UI to support...
jrockowitz’s picture

Status: Active » Needs review

The attached patch adds an 'Edit source' button to the 'Conditional logic' builder. I will follow up with a change record.

jrockowitz’s picture

jrockowitz’s picture

Here is the change record.

jrockowitz’s picture

Some minor cleanup.

  • jrockowitz committed a5a044f on 8.x-5.x
    Issue #2984995 by jrockowitz: Expand the conditionals UI to support...
jrockowitz’s picture

Status: Needs review » Fixed

I committed the patch. Please download the latest dev release to review.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.