1

This is a difficult one to explain. so, I am hoping that someone else has had a similar problem. I am uploading a file via custom configuration form, my field on the form looks like:

$form['homepage_image'] = array(
  '#type' => 'managed_file',
  '#description' => t('The image used on the home, agents, and featured property pages.'),
  '#default_value' => $config->get('pm_admin.homepage_image'),
  '#upload_location' => 'public://upload/siteimages',
  '#required' => TRUE,
);

My submit handler sets the config property in the bog standard way:

$config->set('pm_admin.homepage_image', $form_state->getValue('homepage_image'));

Initially this all works fine, the image is saved and I can grab it and render it where I want in templates. However, after several hours (I have not figured out the trigger) the image disappears. The configuration form no longer has the value. The file is also removed from the file system.

Not sure what is causing this and would appreciate any help.

3

The trigger is the 'status' property of the file. It defaults to temporary, and Drupal clears out temporary managed files on a cron run.

To fix, add this to your form's submit method:

$fid = $form_state->getValue('homepage_image')[0];
$file = \Drupal\file\Entity\File::load($fid);
$file->setPermanent();
$file->save();

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.