When uploading inline images for use in a CKEditor text field, using Drupal's image upload dialogue as provided by the "Image" button in the editor_ckeditor module, the file is initially marked as "temporary" (file_managed.status = 0 in the database).
That is alright, as the idea is that it should be marked as permanent after saving the entity to which the CKEditor field belongs. In the Drupal 8 editor project in issue #1932652: Add image uploading to WYSIWYGs through editor.module they say on this topic and their solution:
Uploads through the WYSIWYG are only going to be stored as temporary files. [Originally] the module […] [would] need to implement a saving mechanism to mark the files as permanent. But now we implement this saving mechanism through entity hooks, so there is (fortunately!) no need anymore for a custom callback that every module has to call. [source]
However, in this Drupal 7 implementation, something is missing, as the files are still marked "temporary" after the entity is saved successfully. This happens everywhere I looked (nodes, custom blocks, …).
Temporary files will be deleted after DRUPAL_MAXIMUM_TEMP_FILE_AGE (usually 6 hours) during the garbage collection cron task. So this issue can lead to data loss.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | editor-inline-images-not-marked-permanent-2695609-8.patch | 17.6 KB | devin carlson |
| #4 | editor-inline-images-not-marked-permanent-2695609-4.patch | 657 bytes | gifad |
Comments
Comment #2
tanius commentedAs a quick workaround, I made a cron job call this command every hour or so:
drush @my-site-alias sql-query "UPDATE file_managed SET status = 1 WHERE SUBSTRING(uri FROM 1 FOR 23) = 'public://inline-images/'"(The command uses MySQL
SUBSTRING()instead of more commonLIKE 'public://inline-images/%'because % has a special meaning in crontab – see.)To work, the above workaround assumes that all editor_ckeditor inline images are saved in the inline-images directory, and that Drupal's retention period for temporary files is longer than the interval between cron runs. This "solution" will also keep unused files around permanently – but as just a workaround …
Comment #3
tanius commentedI think this issue went unnoticed so far because the Drupal garbage collection mechanism does not delete temporary files that are marked as "in use", even if they are older than DRUPAL_MAXIMUM_TEMP_FILE_AGE. Saw messages to that effect when running Drupal cron tasks manually via drush, and some of the temporary inline images on our installation are already weeks old and were not deleted.
So the bug is probably triggered only when the inline image is not correctly marked as "in use". In our case, this happens when using editor_ckeditor inside custom blocks. (Worth another issue report?)
Comment #4
gifad commentedThe attached patch solves this issue the drupal 8 way, as decribed in the issue summary.
It's somewhat a backport of
function _editor_record_file_usage()in core/modules/editor.module.Comment #6
tanius commentedComment #7
Anonymous (not verified) commentedThis doesn't appear to be working for blocks... Can someone else confirm?
Comment #8
devin carlson commentedEditor should handle marking embedded files as permanent, as it does in Drupal core.
Attached is a patch to rework the existing file usage handling, adding support for managed files and a workaround for custom block (which aren't entities in Drupal 7).
Comment #9
devin carlson commentedTested against and committed to Editor 7.x-1.x.