Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Uploading a file to a node without saving the node, the file_insert-function is called. deleting the file directly from the node, file_delete-function is not called. without the callback-function other modules cannot cleanup things they did on insert
i think the problem is in filefield_delete_file_references:
i changed the lines
// Do not update a node if it is already being deleted directly by the user.
if (isset($file->delete_nid) && $file->delete_nid == $nid) {
continue;
}
to
// Do not update a node if it is already being deleted directly by the user.
if (isset($file->delete_nid) && $file->delete_nid == $nid) {
field_file_delete($file);
continue;
}
Comments
Comment #1
quicksketchThanks for the report. Could you clarify the workflow that causes this problem?
Comment #2
aegluke CreditAttribution: aegluke commentedI have a content type with a filefield. uploading a file to a node and then (without saving the node) removed the file via the ajax-upload-formular.
file_insert is called direct on upload. file_delete is never called.
it works only correct if i upload the file - save the node - and then edit the node again and remove the file.
Comment #3
quicksketchPerfect! Thanks.
Comment #4
quicksketchUpon trying to test this out, I could not reproduce a problem. The problem described is that field_file_delete() is not called when removing a file from an unsaved node. However the suggested fix would cause an infinite loop if implemented, since field_file_delete() is the function that calls filefield_delete_file_references() in the first place. The call stack is as follows:
I double checked that hook_file_delete() is called when removing files on unsaved nodes and was unable to reproduce a problem, hook_file_delete() is always called regardless the state of the node or when the file was removed.
Comment #5
teknic CreditAttribution: teknic commentedI think I am able to reproduce this.
I upload a file to a file field, save the node... come back to the node edit page and click remove on the file field upload widget, the hook_file_delete is never called the first time.
Comment #6
quicksketchClicking "Remove" on the file field doesn't fire hook_file_delete() until the node is saved. Even then it's not called if the file is in use multiple places through a module like FileField Sources. It is only called if the file is actually being removed from disk when it is no longer referenced by any content.
Comment #7
teknic CreditAttribution: teknic commentedAh, got it. Thanks.
Comment #8
quicksketch