I am using a webform element 'select_other' in the webform. We got a requirement that the Other field should only appear if certain conditions are satisfied. I already have a Webform Handler In place, and using the alterForm(), I tried to change the #type value, which removed the Other option, but duplicates the field wrapper property.

Is there any way to change the #type of a webform element programmatically from webform_select_other to select?

I was able to get around this by creating a dummy element for select and then in the alterForm, substituting the form element with the new one, using this code.

$type_of_request = $webform_submission->getElementData('type_of_request');

if ($type_of_request == "Process") {
  $webform = $webform_submission->getWebform();
  $dummy_element = $webform->getElement('dummy_element');
  $elements['original_element'] = $dummy_element;

This is a workaround I am using.

  • What are the certain conditions that must be satisfied? It matters.
    – mona lisa
    Commented Apr 12, 2022 at 12:07

1 Answer 1


You should change it to always use a select element, and then make a separate textfield (or whatever type) field for "Other". Then you can use the Webform module's built-in conditionals to control when it shows, without using a webform handler for that.

  1. Add a select element
  2. Add a separate textfield "Other" element
  3. Add a "visible" conditional on the Other element, with the condition being that the type_of_request is set to Process

Example YAML

    '#type': select
    '#title': 'Type of Request'
       review: Review
       process: Process
    '#required': true
    '#type': select
    '#title': 'Select'
       - 'Option 1'
       - 'Option 2'
    '#required': true
    '#type': textfield
    '#title': 'Other'
    '#required': true
        - ':input[name="type_of_request"]':
            value: process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.