[#States API] Nested #states error

Created on 12 December 2023, 12 months ago
Updated 8 April 2024, 8 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.

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 11 months ago
    PHPLint Failed
  • Status changed to Needs review 11 months 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 11 months ago
  • Pipeline finished with Failed
    10 months ago
    Total: 214s
    #100619
  • Pipeline finished with Success
    10 months ago
    Total: 211s
    #100784
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jrockowitz Brooklyn, NY
  • Pipeline finished with Success
    26 days ago
    Total: 474s
    #333443
  • Pipeline finished with Success
    13 days ago
    Total: 515s
    #345316
  • Pipeline finished with Success
    13 days ago
    Total: 646s
    #345967
  • Pipeline finished with Success
    13 days ago
    Total: 544s
    #345991
  • Pipeline finished with Success
    13 days ago
    Total: 730s
    #346023
  • Pipeline finished with Success
    12 days ago
    Total: 541s
    #347031
  • Pipeline finished with Failed
    12 days ago
    Total: 562s
    #347042
  • Pipeline finished with Failed
    12 days ago
    Total: 497s
    #347106
  • Pipeline finished with Failed
    8 days ago
    Total: 559s
    #350872
Production build 0.71.5 2024