1

After uploading a file with the File widget for the File field, the file is on the server and remove only disassociates the file from the field (and the content that the field belongs to).

I would like to add a button to actually delete the file. I've considered a number of options: - via hook_form_alter - but unsure as to what to insert into the $form or $form_state array for the content edit form, regarding new array elements and at what position - by providing a theme .tpl file (based on what devel_themer suggests - tried a few possibilities by the themes did not 'fire' (execute) - making use of the API in https://www.drupal.org/project/extended_file_field to write a hook or otherwise in a custom module

Which of the above would be the standard programmatical approach for extending the file widget to provide direct deletion of the file?

Update I set my debugger to fire on the function file_managed_file_submit($form, &$form_state) that Geoff mentioned. On clicking the Remove button to the right of the file... file field edit

The function file_managed_file_submit($form, &$form_state) is called BUT the status is not zero, so the if fails and the delete does not happen - see debug screen below showing all of this:

file delete does not fire

This commenter has also seen the problem it seems: how to delete the files uploaded in the file field?

Update 2 I think I know what's going on: We have Workbench Moderation on the site. So, it would make sense to not delete the file but rather the remove just disassociates the file from the Current revision of the content. This makes sense if in previous revisions, they did actually need to refer to the file. So by keeping the file present means those previous revisions "integrity" is intact in that they still refer to the file and the link is not broken. So in summary this is 'as design' I think. Thoughts?

(There is a way to properly delete the file through admin/content/file)

0

The file should be deleted, if it isn't there is something going on. When a submit button fires, file_managed_file_submit($form, &$form_state) is triggered:

// No action is needed here for the upload button, because all file uploads on
  // the form are processed by file_managed_file_value() regardless of which
  // button was clicked. Action is needed here for the remove button, because we
  // only remove a file in response to its remove button being clicked.
  if ($button_key == 'remove_button') {
    // If it's a temporary file we can safely remove it immediately, otherwise
    // it's up to the implementing module to clean up files that are in use.
    if ($element ['#file'] && $element ['#file']->status == 0) {
      file_delete($element ['#file']);
    }
    // Update both $form_state['values'] and $form_state['input'] to reflect
    // that the file has been removed, so that the form is rebuilt correctly.
    // $form_state['values'] must be updated in case additional submit handlers
    // run, and for form building functions that run during the rebuild, such as
    // when the managed_file element is part of a field widget.
    // $form_state['input'] must be updated so that file_managed_file_value()
    // has correct information during the rebuild.
    $values_element = $element ['#extended'] ? $element ['fid'] : $element;
    form_set_value($values_element, NULL, $form_state);
    drupal_array_set_nested_value($form_state ['input'], $values_element ['#parents'], NULL);
  }
  • This does not happen for me but thanks for looking into it. I set a break point in my debugger on the file_delete line and it did not hit it. (After clicking the Remove button followed by the content Save button at the foot of the content) And looking at sites/default/files the file is still there. – therobyouknow May 21 '15 at 8:04
  • I've updated my question with a screenshot of the debugger, showing that although the file_managed_file_submit($form, &$form_state) is called, the delete won't happen because an if-check on the status is not zero. I don't know what zero or non status means so any enlightenment on this would be welcome. – therobyouknow May 21 '15 at 8:15
  • Updated question with note that the file_delete does not happen because the status is 1 (not zero). – therobyouknow May 21 '15 at 9:20
  • it would only delete in this function if it's a temp file, otherwise it flags it for the calling function to deal with it properly. If your question edit regarding workbench is correct (which would make sense) it is obviously deciding to ignore the flag and keep the file under some circumstances – Geoff May 21 '15 at 14:21

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.