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

quicksketch’s picture

Thanks for the report. Could you clarify the workflow that causes this problem?

aegluke’s picture

I 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.

quicksketch’s picture

Perfect! Thanks.

quicksketch’s picture

Status: Active » Postponed (maintainer needs more info)

Upon 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:

field_file_delete()
module_invoke_all('file_delete')
filefield_file_delete()
filefield_delete_file_references() // Calling field_file_delete() here would cause a loop.

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.

teknic’s picture

I 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.

quicksketch’s picture

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.

Clicking "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.

teknic’s picture

Ah, got it. Thanks.

quicksketch’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)