0

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->config('mymodule.settings');
    $form['icon1_file'] = [
                '#type'            => 'managed_file',
                '#name'            => 'icon1',
                '#title'           => t('First Icon Image'),
                '#default_value'   => $config->get('icon1_file'),
                '#upload_location' => 'public://',
    ];
}

public function submitForm(array&$form, FormStateInterface $form_state) {
    $imageid = $form_state->getValue('icon1_file');

    $file = file_load($imageid[0]);
    if (gettype($file) == 'object') {
        $file->status = FILE_STATUS_PERMANENT;
    }
    $filename = File::load($imageid[0])->getFilename();
    $this->config('mymodule.settings')
         ->set('icon1_file', $form_state->getValue('icon1_file'))
        ->save();
    $file->save();
    $this->config('mymodule.settings')
         ->set('icon1_filename', $filename)
         ->save();
}

After doing some research I saw that a possible solution might be using \Drupal::service('file.usage')->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->add($file, 'mymodule', 'managed_file', $imageid[0]);

to the code, but it did not resolve my issue.

3

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->status = FILE_STATUS_PERMANENT;  
$file->save();
$file_usage = \Drupal::service('file.usage');
$file_usage->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

enter image description here

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

|improve this answer|||||
2

Have you added this after

$file->save();

? Here is my code and it works with no errors :

$imageID = $config['image'][0];
$file = File::load($imageID);
$file->status = FILE_STATUS_PERMANENT;  
$file->save();
$file_usage = \Drupal::service('file.usage');
$file_usage->add($file, 'mymodule', 'managed_file', $imageID);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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