How can I use a media field in a custom form? I tried something below, which is not working in drupal 8.

$form['upload_documents'] = [
  '#type'          =&gt; 'media',
  '#tree'          =&gt; TRUE,
  '#title'         =&gt; t('Upload your documents'),
  '#default_value' =&gt; ['fid' =&gt; $default_value],
  '#media_options' =&gt; [
    'global' =&gt; [
      'file_extensions' =&gt; 'pdf doc docx',
      'types'           =&gt; ['document'],
    ],
  ],
  '#attached'      =&gt; [],
  '#extended'      =&gt; TRUE,
  '#required'      =&gt; TRUE,
];
I found the answer to create the media autocomplete field like below.

// $id is your media ID.
$media = \Drupal\media_entity\Entity\Media::load($id); 

$form['upload_doc'] = [
  '#type'          =&gt; 'entity_autocomplete',
  '#target_type'   =&gt; 'media',
  '#default_value' =&gt; $media,
];