Select other computed token error

Created on 24 March 2023, over 1 year ago
Updated 26 June 2024, 2 days ago

With a multipage form, using a computed token field containing a token for a 'Select Other element', get the following error:

Warning: Array to string conversion in Drupal\webform\Utility\WebformOptionsHelper::getOptionText() (line 131 of modules\contrib\webform\src\Utility\WebformOptionsHelper.php).

Drupal\webform\Utility\WebformOptionsHelper::getOptionText(Array, Array, ) (Line: 411)
Drupal\webform\Plugin\WebformElement\OptionsBase->formatTextItem(Array, Object, Array) (Line: 1374)
Drupal\webform\Plugin\WebformElementBase->format('Text', Array, Object, Array) (Line: 1331)
Drupal\webform\Plugin\WebformElementBase->formatText(Array, Object, Array) (Line: 234)
Drupal\webform\Plugin\WebformElementManager->invokeMethod('formatText', Array, Object, Array) (Line: 1073)
_webform_token_get_submission_value('field_610', Array, Object, Object, Object) (Line: 743)

Changing the field to a standard Select (without Other), the error goes away.

πŸ› Bug report
Status

RTBC

Version

6.2

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom alangallery

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

Merge Requests

Comments & Activities

  • Issue created by @alangallery
  • πŸ‡ΊπŸ‡ΈUnited States cilefen

    Please provide the yaml of a minimal form that exhibits the bug.

  • πŸ‡¬πŸ‡§United Kingdom alangallery

    Thanks cilefen

    Creating a simple multi-paged test form with select other element didn't give errors, but adding a conditional visibility to the select other element did. So it seems to be related to have conditions in the form.

    The form I'm creating has lots of visibility/required conditions on elements, but not actually on the select other element.

    first_page:
      '#type': wizard_page
      '#title': 'First page'
      standard_select_question:
        '#type': select
        '#title': 'Standard select question'
        '#options': yes_no
        '#required': true
    second_page:
      '#type': wizard_page
      '#title': 'Second page'
      do_you_want_to_answer_the_next_question:
        '#type': select
        '#title': 'Do you want to answer the next question'
        '#options': yes_no
      select_with_other_question:
        '#type': select_other
        '#title': 'Select with other question'
        '#options': yes_no
        '#other__option_label': Other
        '#other__placeholder': 'Please enter the preferred pronoun'
        '#states':
          visible:
            ':input[name="do_you_want_to_answer_the_next_question"]':
              value: 'Yes'
    third_page:
      '#type': wizard_page
      '#title': 'Third page'
      computed_tokens:
        '#type': computed_token
        '#title': 'Computed tokens'
        '#mode': text
        '#template': '"[webform_submission:values:standard_select_question]","[webform_submission:values:do_you_want_to_answer_the_next_question]","[webform_submission:values:select_with_other_question]"'
        '#store': true
  • πŸ‡¬πŸ‡§United Kingdom alangallery

    Further information - if rendering token as raw value [webform_submission:values:select_with_other_question:raw]

    Get two errors showing on form:
    User error: "select" is an invalid render array key in Drupal\Core\Render\Element::children() (line 98 of core\lib\Drupal\Core\Render\Element.php).

    User error: "other" is an invalid render array key in Drupal\Core\Render\Element::children() (line 98 of core\lib\Drupal\Core\Render\Element.php).

    Both regarding the token [webform_submission:values:select_with_other_question:raw]

    _webform_token_get_submission_value('select_with_other_question:raw', Array, Object, Object, Object) (Line: 743)
    webform_tokens('webform_submission', Array, Array, Array, Object)
  • I ran into this issue too. Doing some debugging with XDebug, it seems the error is coming from Drupal\webform\Plugin\WebformElement\OptionsBase::formatTextItem/formatHtmlItem. The call to getValue is returning an array of ["select" => [...], "other" => "..."] for some reason, but only when called from inside a computed token/computed twig field and when the select element is conditionally visible.

    When the select element isn't conditional/when the token is in the confirmation page, it seems to be returning a string (either the option selected, or the value of the Other field), as expected.

    I was able to do an ugly workaround by using a computed twig field, by detecting if it's an array and returning an empty string, otherwise computing the token:

    {{ data.select_element is iterable ? '' : webform_token('[webform_submission:values:select_element]', webform_submission) }}

  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    The attached webform replicates the issue using the elements from #3.

  • Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.2 & MySQL 8
    last update about 1 month ago
    Waiting for branch to pass
  • Pipeline finished with Failed
    about 1 month ago
    Total: 1819s
    #173899
  • Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.2 & MySQL 8
    last update about 1 month ago
    Waiting for branch to pass
  • Pipeline finished with Success
    about 1 month ago
    Total: 3139s
    #173913
  • Status changed to Needs review about 1 month ago
  • @jrockowitz Thanks for the repro config.

    I've submitted an MR with tests that fixes this issue. Interestingly, there was already code to handle a similar bug in OptionsBase::getElementSelectorInputValue, from issue #3000202 β†’ . I pulled that code out to an overriden getValue function which should fix the bug for all code paths including OptionsBase::formatTextItem/formatHtmlItem mentioned above, which is the source of the error in this issue.

  • We've now been using the fix in MR #469 in production for a month without any errors, and have been able to remove our workarounds. It'd be great if someone else could test and change this issue to RTBC!

  • πŸ‡«πŸ‡·France jmaxant

    Tested today, works on a clean install & ran "test-only changes" job, which shows coverage. Everything looks fine by me !
    I'll change it to RTBC shortly if no one else does in the meantime.

  • Status changed to RTBC 2 days ago
  • πŸ‡«πŸ‡·France jmaxant
Production build 0.69.0 2024