Problem/Motivation

When setting the conditional on a webform field, nested states are giving an error.

Steps to reproduce

Create a conditional webform field, the webform field definition which breaks is:

  assistant_dean_approval:
    '#type': fieldset
    '#title': 'Assistant Dean Approval'
    '#states':
      visible:
        -
          - ':input[name="assistant_dean_email"]':
              value: '[current-user:mail]'
        - or
        -
          - ':input[name="area_of_access"]':
              value: some_area
          - and
          - ':input[name="current_logged_in_user"]':
              value: some_user

Adding this on the Build|Source tab and saving does not give an error, but if I attempt to edit the element on the Build|Elements tab, ajax fails and the field dialog is not displayed. Looking into the logs, the error given is:

TypeError: Drupal\webform\Element\WebformElementStates::getStatesArrayCondition(): Argument #2 ($condition) must be of type array, string given, called in /opt/drupal/web/modules/contrib/webform/src/Element/WebformElementStates.php on line 795 in Drupal\webform\Element\WebformElementStates::getStatesArrayCondition() (line 815 of /opt/drupal/web/modules/contrib/webform/src/Element/WebformElementStates.php).

If I remove the nested '- and' condition, then there is no error and the element can be edited on the Build|Elements tab, but it does not behave as desired.

Note: https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields... indicates that this type of nesting should be valid.

Proposed resolution

modify webform/src/Element/WebformElementStates.php to properly handle nested conditions.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork webform-3408243

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

kwfinken created an issue. See original summary.

jrockowitz’s picture

Did you try to remove the 'and' and use...

      visible:
        -
          - ':input[name="assistant_dean_email"]':
              value: '[current-user:mail]'
        - or
        -
          ':input[name="area_of_access"]':
            value: some_area
          ':input[name="current_logged_in_user"]':
            value: some_user
kwfinken’s picture

Thank you for the prompt response:

I have tried these variations, they save and edit without problem, but do not function properly.

visible:
  - ':input[name="assistant_dean_email"]':
      value: '[current-user:mail]'
  - or
  - ':input[name="area_of_access"]':
      value: some_access
    ':input[name="current_logged_in_user"]':
      value: some_user

and

visible:
  - ':input[name="assistant_dean_email"]':
      value: '[current-user:mail]'
  - or
  - 
    - ':input[name="area_of_access"]':
        value: some_access
    - ':input[name="current_logged_in_user"]':
        value: some_user

For others facing the same problem, I did get things working properly by using a hidden computed twig field and then referencing that as the condition. So my problem is solved, but the fact that this is not working is not solved.

The way that worked is:

  ad_for_dsl_access:
    '#type': computed_twig
    '#title': 'AD for DSL Access'
    '#attributes':
      class:
        - d-none
    '#mode': text
    '#template': |-
      {% if webform_token('[current-user:account-name]', webform_submission, [], options) == 'some_user' and
      data.area_of_access == 'some_access' %}TRUE{% endif %}
    '#whitespace': spaceless
    '#store': true
    '#ajax': true
  assistant_dean_approval:
    '#type': fieldset
    '#title': 'Assistant Dean Approval'
    '#states':
      visible:
        - ':input[name="assistant_dean_email"]':
            value: '[current-user:mail]'
        - or
        - ':input[name="ad_for_dsl_access"]':
            value: 'TRUE'

abhishek_gupta1 made their first commit to this issue’s fork.

abhishek_gupta1’s picture

Status: Active » Needs review

@kwfinken, I've incorporated updates addressing your concern. Please review the attached patch file for the modifications.

cilefen’s picture

Status: Needs review » Needs work
jrockowitz’s picture

Title: Nested #states error » [#States API] Nested #states error
Version: 6.2.2 » 6.2.x-dev
jrockowitz’s picture

Status: Needs work » Closed (works as designed)

There is support for nested conditions with test coverage

@see https://git.drupalcode.org/project/webform/-/blob/6.3.x/tests/modules/we...

I was not able to replicate the original issue which has a workaround.

I don't see other people running into this issue.