Score:0

How can I get conditional logic working when embedding a webform?

ve flag

When embedding a multi-page webform within a render array using #type => 'webform', it seems like there's a bug where the conditional logic of webform elements does not work.

Note: The webform's conditional logic does work when I view the webform in webform's actual "View" and "Test" pages.

Furthermore, when embedding a webform on a page using a block, the conditional logic also works (but the page refreshes between the page).

On a side note, when embedding the multi-page webform in a form (using the code below), the page isn't refreshed.

Here's my webform yaml:

...
elements: |-
  page_1:
    '#type': webform_wizard_page
    '#title': 'Page 1'
    show_textbox:
      '#type': checkbox
      '#title': 'Show textbox'
    textbox:
      '#type': textarea
      '#title': Textbox
      '#states':
        visible:
          ':input[name="show_textbox"]':
            checked: true
  page_2:
    '#type': webform_wizard_page
    '#title': 'Page 2'
    show_textbox2:
      '#type': checkbox
      '#title': 'Show textbox'
    textbox2:
      '#type': textarea
      '#title': Textbox
      '#states':
        visible:
          ':input[name="show_textbox2"]':
            checked: true
...

Also, here's the Drupal Form class I'm using to embed the webform:

<?php

namespace Drupal\test_webform_conditionals\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class TestForm extends FormBase
{

  public function getFormId()
  {
    return 'test_webform_conditionals';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $form['#tree'] = TRUE;
    $form['webform'] = [
    '#type' => 'webform',
    '#webform' => 'test_conditionals', // webform id goes here
    '#lazy' => TRUE // If this is false, conditionals won't work at all. When it's true, at least the conditionals only work for the elements in the first page of the webform. But the conditional logic for the elements on the second and further pages won't work
    ];
    return $form;
  }

  public function validateForm(array &$form, FormStateInterface $form_state)
  {
  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
  }
}

Am I doing something wrong when embedding a multi-page webform with conditional logic?

Thanks!

id flag
Are there errors in browser console? What, technically, does "works" and "does not work" in this question? Can you be more precise about that aspect please?
id flag
I’m voting to close this question because it is reported, verbatim, as a bug https://www.drupal.org/project/webform/issues/3375670. It can’t be both a question and a bug.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.