How to get the file ID using managed_file on custom form? In Drupal 8, I have a custom form that I built using the Form API in a custom module. I am trying to upload a file using managed_file field. For some reason I can't get it to give me the file ID no matter what code I try. The file does indeed get uploaded and saved, but only as a temporary file.

Please see below for the code I am using. The value of the form field is always NULL.

buildForm:

public function buildForm(array $form, FormStateInterface $form_state, $event = NULL) {
  $validators = array(
    'file_validate_extensions' =&gt; array('jpg jpeg png gif'),
  );
  $form['event_image'] = array(
    '#type' =&gt; 'managed_file',
    '#name' =&gt; 'event_image',
    '#title' =&gt; t('Event Image'),
    '#upload_validators' =&gt; $validators,
    '#upload_location' =&gt; 'public://event-images/',
  );

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; $this-&gt;t('Save'),
  );

  return $form;
}


submitForm:

public function submitForm(array &amp;$form, FormStateInterface $form_state) {
  $event_image = $form_state-&gt;getValue('event_image');
  if ($event_image) {
    $file = File::load(reset($event_image));
    $file-&gt;setPermanent();
    drupal_set_message('Filename: ' . $file-&gt;getFilename());
  }
  ksm($event_image); // Returns empty array??
}
I think you need to save the file :
public function submitForm(array &amp;$form, FormStateInterface $form_state) {
  $event_image = $form_state-&gt;getValue('event_image');
  if ($event_image) {
    $file = File::load(reset($event_image));
    $file-&gt;setPermanent();
    $file-&gt;save();
    drupal_set_message('Filename: ' . $file-&gt;getFilename());
  }
}