Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedTypically, 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.