For managed file not added file usage

Migrated issue

Reported by: notifyone

Problem/motivation

Confirmed on 8.x-1.10. Files uploaded through a managed_file attribute are saved as temporary files. The module stores the file ID in the link options but never marks the file permanent or records file usage. Cron deletes the file after system.file:temporary_maximum_age (six hours by default), and the attribute points to a file that no longer exists.

Steps to reproduce

  1. Add an attribute with type: managed_file.
  2. Upload a file on a menu link and save.
  3. The file entity has status 0 (temporary) and no entry in file_usage.
  4. Run cron after the temporary age has passed: the file is gone.

Proposed resolution

In menu_link_attributes_menu_link_content_form_entity_builder(), or in a menu_link_content presave/delete hook, for every managed_file attribute:

  • mark newly referenced files permanent and add usage with file.usage (menu_link_attributes, menu_link_content, link ID);
  • remove usage for files no longer referenced, and for all files when the link is deleted.
Edited by Norman Kämper-Leymann