'File used may not be referenced' if form field submits same image twice I've built a custom module with a form that allows users to define custom site wide settings such as the icons that are meant to be used.

I'm able to upload and save images into the form using managed_file elements. However if I resubmit the form without changing what files are used I receive the message:


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


First Icon Image was the name of my field in that case. Everytime I change the image used I don't receive the image. It's only when I submit the form again with the same values for the image fields.

I'm handling the image field with the following code:

public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this-&gt;config('mymodule.settings');
    $form['icon1_file'] = [
                '#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://',
    ];
}

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;
    }
    $filename = File::load($imageid[0])-&gt;getFilename();
    $this-&gt;config('mymodule.settings')
         -&gt;set('icon1_file', $form_state-&gt;getValue('icon1_file'))
        -&gt;save();
    $file-&gt;save();
    $this-&gt;config('mymodule.settings')
         -&gt;set('icon1_filename', $filename)
         -&gt;save();
}


After doing some research I saw that a possible solution might be using \Drupal::service('file.usage')-&gt;add however I'm not sure what I'd use for the entity_type and entity_id parameters if that, indeed, is the solution.

How would I prevent the "File used in the field may not be referenced" when occurs on the second time I submit the form with the same file?

UPDATE: I added

$file_usage = \Drupal::service('file.usage');


and 

$file_usage-&gt;add($file, 'mymodule', 'managed_file', $imageid[0]);


to the code, but it did not resolve my issue.
Using the skumy's approach worked for me, but instead of putting 'managed_file' as the third parameter for $file_usage->add(), I put 'file'.

$fid = $config['image'][0];
$file = File::load($fid);
$file-&gt;status = FILE_STATUS_PERMANENT;  
$file-&gt;save();
$file_usage = \Drupal::service('file.usage');
$file_usage-&gt;add($file, 'mymodule', 'file', $fid);


That's because putting 'managed_file' causes an error when I check the file usages, following the links in the "Used in" column in /admin/content/files, since in my installation does not exist any entity called 'managed_file'.


  The "managed_file" entity type does not exist




Using 'file' instead of 'managed_file' solves the problem.