How to use the managed_file field in a custom form plugin I'm attempting to create a module with a custom form that allows users to upload a file using a managed_file field. The name of my module is foundation_settings and the file structure is:

foundation_settings
  -foundation_settings.info.yml
  -foundation_settings.module
  -src
    -Form
      -FoundationForm.php


Inside FoundationForm.php I'm building the form with the following code:

public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this-&gt;config('foundation.settings');
        $form['icons'] = [
            '#type'            =&gt; 'details',
            '#title'           =&gt; t('Front Page Icons'),
            '#open'            =&gt; FALSE,
            'icon1'            =&gt; [
                '#type'           =&gt; 'fieldset',
                '#title'          =&gt; t('First Icon'),
                'icon1_file'        =&gt; [
                    '#type'            =&gt; 'managed_file',
                    '#name'            =&gt; 'icon1',
                    '#title'           =&gt; t('First Icon Image'),
                    '#default_value'   =&gt; $config-&gt;get('icon1_file'),
                    '#upload_location' =&gt; 'public://',
                ],
            ],
       ];
}


However I'm not sure how to handle the managed_file when the form is being submitted. In my submitForm function I have

public function submitForm(array&amp;$form, FormStateInterface $form_state) {
    $imageid = $form_state-&gt;getValue('icon1_file');
    $file = file_load($imageid[0]);
        if (gettype($file) == 'object') {
            $file-&gt;status = FILE_STATUS_PERMANENT;
        }
        $this-&gt;config('foundation.settings')
             -&gt;set('icon1_file', $form_state-&gt;getValue('icon1_file'))
            -&gt;save();
            $file-&gt;save();
}


The upload and saving works okay but I keep receiving this error after submitting


  The file used in the First Icon Image field may not be referenced.


How are you supposed to use managed_file field in custom form plugins?
I think the issue we had was understanding the data that the form was sending back to us when using the 'managed_file' type. This creates an entity of File and stores the image information directly into the table called 'file_managed' in your database, but also saves the file to your directories based upon where you send it. So take this code for example:

$form['mainslider_slide_one']['image_dir'] = [
    '#type'                 =&gt; 'managed_file',
    '#upload_location'      =&gt; 'public://module-images/home-slider-images/',
    '#multiple'             =&gt; FALSE,
    '#description'          =&gt; t('Allowed extensions: gif png jpg jpeg'),
    '#upload_validators'    =&gt; [
      'file_validate_is_image'      =&gt; array(),
      'file_validate_extensions'    =&gt; array('gif png jpg jpeg'),
      'file_validate_size'          =&gt; array(25600000)
    ],
    '#title'                =&gt; t('Upload an image file for this slide')
];


For the '#upload_location' we are telling the file to be stored in our public folder under the specified directory (sites/sitename/files/ directory). So now we have the actual file stored where we want, as well as data stored in the database.

When we receive the information back (under validation for instance) we have the following code:

public function validateForm(array &amp;$form, FormStateInterface $form_state) {
    $managedFileId_imageOne = $form_state-&gt;getValue(['mainslider_slide_one' =&gt; 'image_dir']);

    if (empty($managedFileId_imageOne)) {
        $form_state-&gt;setErrorByName('test', 'No image found for image one');
    }
}


The result of $managedFileId_imageOne will either be an empty array (whereas I am sending an error message back) or an array of fid's.  The fid is the location in the database.  This is the information that we ended up saving into our settings file data. So when we pull the information into our program we are querying for the 'fid' located in the 'file_managed' table using the Entity::load method.

NOTE/UPDATE: 

Don't forget that files uploaded in this manner are currently, by default, set to be temporary (status column in the "file_managed" table). You need to set the file to permanent to manually set status to 1 so they are not temporary. I believe this is functionality that is currently a bug and they are looking into possibly updating this in future releases.

Sample from Jasodeep

   $image = $form_state-&gt;getValue('image');
   $file = File::load( $image[0] );
   $file-&gt;setPermanent();
   $file-&gt;save();


Hope this helps at least get you on your way!