Problem/Motivation

I can't find documentation about how to add a "Select Other" element. I have used Webform's custom element example:

class WebformExampleElement extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#size' => 60,
      '#process' => [
        [$class, 'processWebformElementExample'],
        [$class, 'processAjaxForm'],
      ],
      '#element_validate' => [
        [$class, 'validateWebformExampleElement'],
      ],
      '#pre_render' => [
        [$class, 'preRenderWebformExampleElement'],
      ],
      '#theme' => 'input__webform_example_element',
      '#theme_wrappers' => ['form_element'],
    ];
  }

but this creates a textfield and not a select other. I have tried to add:

 public function getInfo() {
    $class = get_class($this);
    return [
      '#type' => 'webform_select_other',
      '#options' => array(
        '6m' => '6 months',
        '1y' => '1 year'
      ),

with no avail. I even tried add this to:

  public static function processWebformElementExample(&$element, FormStateInterface $form_state, &$complete_form) {
    $element['#type'] = 'webform_select_other';
    $element['#options'] = array(
      '6m' => '6 months',
      '1y' => '1 year'
    );
    return $element;
  }

with no avail. Any help with how to add a select other rather than a textfield. Thank you!

Comments

asilva3 created an issue. See original summary.

jrockowitz’s picture

Status: Active » Closed (outdated)
The Webform module's issue queue is for bugs, feature requests, and planning. Please post general support questions to Drupal Answers. You can also ask questions or help answer questions on the #webform channel on Drupal Slack.

Typically, you need to add the select other as a sub element in processWebformElementExample but you also have use a validate callback the massage the submission value. You can look at \Drupal\msk_elements\Element\EmailConfirm for an example of this sub-element pattern.

public static function processWebformElementExample(&$element, FormStateInterface $form_state, &$complete_form) {
    $element['#tree'] = true
    $element['other']['#type'] = 'webform_select_other';
    $element['other']['#options'] = array(
      '6m' => '6 months',
      '1y' => '1 year'
    );
    return $element;
  }