Add a Radio-button with other option in comment type I have the following requirement in Drupal 8:

I have to create a comment type with options. In options there will be a Other option on whose click a text field will be opened.

something like below image:


How can i achieve the showing of text box on other option selection in comment type?
I think you need to do this in a hook_form_alter.

I have written some example code that you can use. The code is probably not 100% correct, but it's intended to help you, to get an idea of how to solve the problem.

function MY_MODULE_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface  $form_state, $form_id) {
  $form['radios_field']['#ajax'] = [
    'callback' =&gt; 'MY_MODULE_radios_field_callback',
    'event' =&gt; 'change',
    'wrapper' =&gt; 'radio-field-wrapper',
  ];

  if ($form_state-&gt;getValue('radiois_field') == 'other') {
    // Field to show when options is "Other".
    $form['textfield'] = [
      '#type' =&gt; 'textfield',
      '#attributes' =&gt; [
        'id' =&gt; 'radio-field-wrapper',
      ],
    ];
  }
}

function MY_MODULE_radios_field_callback(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form_state-&gt;setRebuild();
  return $form['radios_field'];
}