Added following code to my custom Form and nothing is rendered when form is displayed.

$form['my_field'] = array(
'#type' => 'select_or_other',
'#title' => t('Choose an option'),
'#default_value' => array('value_1'),
'#options' => array(
'value_1' => t('One'),
'value_2' => t('Two'),
'value_3' => t('Three'),
),
'#other' => t('Other (please type a value)'), // Text to show as 'other' option
'#required' => TRUE,
'#multiple' => TRUE,
'#other_unknown_defaults' => 'other', // If the #default_value is not a valid choice in #options, what should we do? Possible values 'append', 'ignore', 'other' (if 'other' specified you can also override #other_delimiter).
'#other_delimiter' => ', ', // Delimiter string to delimit multiple 'other' values into the 'other' textfield. If this is FALSE only the last value will be used.
'#select_type' => 'select', // Defaults to 'select'. Can also be 'radios' or 'checkboxes'.
);

If I change '#type' to 'select' field gets rendered as normal select field.

Any ideas?

Comments

drupalbigot created an issue. See original summary.

drupalbigot’s picture

Priority: Major » Normal
sneha_chandole’s picture

Hi,

You can use below code for form api
$form['my_field'] = array(
'#type' => 'select',
'#title' => t('Choose an option'),
'#default_value' => array('value_1'),
'#options' => array(
'value_1' => t('One'),
'value_2' => t('Two'),
'value_3' => t('Three'),
),
'#other' => t('Other (please type a value)'), // Text to show as 'other' option
'#required' => TRUE,
'#multiple' => TRUE,
'#other_unknown_defaults' => 'other', // If the #default_value is not a valid choice in #options, what should we do? Possible values 'append', 'ignore', 'other' (if 'other' specified you can also override #other_delimiter).
'#other_delimiter' => ', ', // Delimiter string to delimit multiple 'other' values into the 'other' textfield. If this is FALSE only the last value will be used.
'#select_type' => 'select', // Defaults to 'select'. Can also be 'radios' or 'checkboxes'.
);

You can use either select, radios or checkboxes type. you can not use select_or_other as type of the form. you can check form api for drupal 8 and related types of the form.
Thanks.

Dubs’s picture

This works for me: -

 $form['my_field'] = array(
      '#type' => 'select_or_other_select',
      '#title' => t('Choose an option'),
      '#default_value' => array('value_1'),
      '#options' => array(
        'value_1' => t('One'),
        'value_2' => t('Two'),
        'value_3' => t('Three'),
      ),
      '#other' => t('Other (please type a value)'),   // Text to show as 'other' option
      '#required' => TRUE,
      '#other_unknown_defaults' => 'other', // If the #default_value is not a valid choice in #options, what should we do? Possible values 'append', 'ignore', 'other'  (if 'other' specified you can also override #other_delimiter).
      '#other_delimiter' => ', ', // Delimiter string to delimit multiple 'other' values into the 'other' textfield.  If this is FALSE only the last value will be used.
      '#select_type' => 'select', // Defaults to 'select'.  Can also be 'radios' or 'checkboxes'.
    );
legolasbo’s picture

Version: 8.x-3.x-dev » 8.x-1.x-dev

8.x-3.x-dev is not under development.

daften’s picture

Version: 8.x-1.x-dev » 4.x-dev
Status: Active » Fixed

I updated the link to the External documentation to point to the README file, for which the example does work. Feel free to re-open with a patch if something is still missing.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.