I have two radio buttons

  • Enter The Text
  • Upload a Text File

If user select the 1st option, a field of type 'textfield' will appear & if they select the 2nd option, a field of type 'managed_file' will appear.

But this code with working fine for textfield but not working for managed_file
Now, how to implement conditional fields in Drupal 8 ?

This is my D8 code.

    $form['field_type'] = array(
    '#type'    => 'radios',
    '#options' => array_combine( array(t('Enter The Text'), t('Upload a Text File'))
                                 array(t('Enter The Text'), t('Upload a Text File'))
    '#title'   => t('Choose entry type.')

  $form['imgurl'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Enter the text'),
    '#states'        => array(
      'visible'      => array(
        ':input[name="field_type"]' => array('value' => t('Enter The Text')),

  $form['file'] = array(
    '#title'              => t('Choose Text File'),
    '#type'               => 'managed_file',
    '#progress_indicator' => 'throbber',
    '#progress_message'   => NULL,
    '#upload_location'    => 'public://files/',
    '#states'             => array(
      'visible'           => array(
        ':input[name="field_type"]' => array('value' => t('Upload a Text File')),

I don't know, if this as anything to do with managed files, but I think #options should look like this:

'#options' => array(
  'text' => t('Enter The Text'),
  'upload' => t('Upload a Text File'),

and in #states the options key, not the translatable value:

'#states' => array(
  'visible' => array(
    ':input[name="field_type"]' => array('value' => 'text'),

If it has something to do with managed_file, then this old issue is back:


You can check this by changing temporarily managed_file to file.

| improve this answer | |
  • Tried your solution. Having the same problem. 'managed_file' type field is not hiding on selecting the other option. – Jasodeep Chatterjee May 3 '16 at 12:40
  • There was an issue with this, see the edited answer. – 4k4 May 3 '16 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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