1

In a custom module (amap), I have a custom content entity (contract) created with Drupal Console.
There are standard CRUD functions on this entity.
One of the fields of the entity is a file field:

$fields['file1'] = BaseFieldDefinition::create('file')
  ->setLabel(t('Source file'))
  ->setDescription(t('Source file (may be modified)'))
  ->setSettings([
    'target_type' => 'file',
    'file_extensions' => 'doc docx odt pages',
    'uri_scheme' => 'private',
    'file_directory' => 'contracts',
  ])

When I create one contract adding a file (a.pages) to the entity, everything works as expected: a file with permanent status is added to file_managed table and appears in Files tab of content page:
enter image description here
enter image description here

and the corresponding record is added to file_usage table: enter image description here My first problem appears when I want to update the contract changing the file (to Contract.doc, for example):
file_usage table is correctly updated: enter image description here but file_managed table is not! The previous file (a.pages) is still there with permanent status.
enter image description here The second problem is quite the same: when I delete the contract, file_usage table is correctly updated: there is no more record related to these files.
But file_managed table is not! The previous file (a.pages) is still there with permanent status (as in update mode).

My question is: is is normal?
Do I have to handle file_managed table myself?
Why is file_usage correctly updated and not file_managed?
I naively thought it was the case...

  • 1
    I believe there is a hidden core flag now for files that is named something like “mark unused files as temporary” and it defaults to FALSE. I don’t have any code in front of me so I’m not 100%. But I just fixed something similar to your case. – Kevin Apr 8 at 13:03
3

I changed file.settings config entry make_unused_managed_files_temporary to true and it seems to do the job.
Just hope there won't be any side effects...

  • The default for that setting is FALSE because some sites were having their files unexpectedly deleted when usage wasn't accurately updated (e.g. I had encountered this with a WYSIWYG editor's file uploader that didn't add a usage entry). – gapple Apr 8 at 16:29
  • Yeah, thats exactly why it was added. – Kevin Apr 8 at 16:47
  • So, I can use it if I ensure that usage is properly managed? – gbmapo Apr 8 at 17:24

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.