[#States API] Nested #states error

Created on 12 December 2023, about 1 year ago
Updated 8 April 2024, 10 months ago

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

🐛 Bug report
Status

Needs work

Version

6.2

Component

Code

Created by

🇺🇸United States kwfinken Lansing, MI

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @kwfinken
  • 🇺🇸United States jrockowitz Brooklyn, NY

    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
    
  • 🇺🇸United States kwfinken Lansing, MI

    Thank you for the prompt response:

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

    <pre>
    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
    </pre>
    

    and

    <pre>
    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
    </pre>
    
    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:

    <pre>
      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'
    </pre>
    
  • 🇮🇳India abhishek_virasat

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

  • Merge request !390fix the issue of Nested #states error → (Open) created by abhishek_virasat
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    PHPLint Failed
  • Status changed to Needs review about 1 year ago
  • 🇮🇳India abhishek_virasat

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

  • Status changed to Needs work about 1 year ago
  • Pipeline finished with Failed
    12 months ago
    Total: 214s
    #100619
  • Pipeline finished with Success
    12 months ago
    Total: 211s
    #100784
  • 🇺🇸United States jrockowitz Brooklyn, NY
  • Pipeline finished with Success
    3 months ago
    Total: 474s
    #333443
  • Pipeline finished with Success
    3 months ago
    Total: 515s
    #345316
  • Pipeline finished with Success
    3 months ago
    Total: 646s
    #345967
  • Pipeline finished with Success
    3 months ago
    Total: 544s
    #345991
  • Pipeline finished with Success
    3 months ago
    Total: 730s
    #346023
  • Pipeline finished with Success
    3 months ago
    Total: 541s
    #347031
  • Pipeline finished with Failed
    3 months ago
    Total: 562s
    #347042
  • Pipeline finished with Failed
    3 months ago
    Total: 497s
    #347106
  • Pipeline finished with Failed
    2 months ago
    Total: 559s
    #350872
  • Pipeline finished with Skipped
    29 days ago
    #388935
  • Pipeline finished with Success
    27 days ago
    Total: 171s
    #390607
  • Pipeline finished with Canceled
    27 days ago
    Total: 213s
    #390610
  • Pipeline finished with Success
    27 days ago
    Total: 165s
    #390613
  • Pipeline finished with Success
    27 days ago
    Total: 160s
    #390615
Production build 0.71.5 2024