I am have been struggling with this for the past day or so. I am simply trying to create a custom entity and populate/or update the image field with the fid. Whats weird is that every other form field I am passing I can get the value for but this managed_file field for uploading an image always returns NULL when I call dpm().

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

Within my form_build function, I use the following code.

// Manage file.
$form['theimage'] = array(
  '#type' => 'managed_file',
  '#title' => 'Managed file',
  '#description' => $this->t('Manage file, #type = managed_file'),
  //'#default_value' => $form_state->getValue('theimage'),
  '#upload_location' => 'public://images/',
  '#required' => FALSE,

In the submission handler, I use the following code.

// Get the fid value, upload it and save it to the managed file table. 
$fid = $form_state->getValue(['theimage', 0]);
if (!empty($fid)) {
  $file = File::load($fid);

Any thoughts, solutions, or recommendations would be greatly appreciated.

  • 1
    i havent worked with drupal for almost a year and i dont have exact piece of code for reference at hand but if i rememebr correctly the the file field output can be three different types: fileid, [0=>fileid] or [fids => [0 => fileid]] or something like that. So debug the field directly, not the 0 sub-value and you'll what yo uare getting...or better yet the thole form state values array.
    – user21641
    Commented Aug 12, 2018 at 22:40
  • 1
    As @IvanJaros said first debug the field either using dpm($form_state->getValues()) or dpm($form_state->getValue(['theimage'])). Then you may get how the structure of managed file is being rendered with FID.
    – Ajay Reddy
    Commented Aug 13, 2018 at 7:11
  • Thanks @IvanJaros .Your suggestion definitely put me on the right path.
    – cthrucode
    Commented Aug 13, 2018 at 17:25
  • Thanks @AjayReddy. Your suggestion definitely put me on the right path.
    – cthrucode
    Commented Aug 13, 2018 at 17:25

2 Answers 2


After alot of digging/debugging i discovered that i could not pull any data from the "manage_file" field while using the PrivateTempStore in my Multi-Step form. After disabling the form cache, i began to get the values and the expected behavior by using:


My Submit Handler

public function buildForm(array $form, FormStateInterface $form_state) {
 *  Fixes image upload bug in form. Does not work unless we 
 disable the cache while using PrivateTempStore




See https://www.drupal.org/project/drupal/issues/2647812#comment-11683961

  • A word of warning here: disabling the cache like this also disables adding elements dynamically such as paragraph types or fields with multiple values.
    – ofrommel
    Commented Jul 21, 2019 at 22:09

$form_state will show NULL or empty arrays in the file upload if you don't use the proper upload function. The file hasn't been uploaded yet.

In the validation function, for example, you should use the function file_save_upload.

 * {@inheritdoc}
public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  $file = file_save_upload('banner_image', array(), FALSE, 0, FILE_EXISTS_REPLACE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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