I've a file field attached to a node form. I want to know in the submit handler if the file has been replaced then I'll take some action.

For example I delete the previously uploaded file : abc.txt and then upload a new one "newdoc.txt" in place of it and "Submit".

I don't want to load the node and read it's contents or use hook_presave and read the value using $node->original.

  • Do you mean incase of node edit ? Why you don't want to load the node and read it's contents or use hook_presave and read the value using $node->original ?
    – Anil Sagar
    Apr 16 '14 at 5:57
  • If those old values are already available in the $form_state then why query the database the long way. Can I get it from the $form or $form_state?
    – AgA
    Apr 16 '14 at 5:59
  • I believe if you do hook_form_alter and add extra submit handler.. In your submit handler $node is part of either $form or $form_state.. Did you verify that ?
    – Anil Sagar
    Apr 16 '14 at 6:05
  • Thanks you're right. There is 'node' in form_state which shows the old content. I was confused as dsm shows it as some recursion instead of the node content. But dsm($form_state['node']) shows fine.
    – AgA
    Apr 16 '14 at 6:16
  • Posted same few minutes back as answer :-)
    – Anil Sagar
    Apr 16 '14 at 6:21

I have just verified it... Form submit handler does store old values and new values..

In $form you see old fid in #node property , In $form_state['values'] you see new fid that was uploaded...

enter image description here enter image description here

By comparing above two in your submit handler you can take action..

function my_custom_submit_file_alter_detector($form, &$form_state) {
  • Where can I find current filename? Since in form_state['values']['file_fieldname'] it simply gives fid. And form_state arrays are unendingly long.
    – AgA
    Apr 16 '14 at 6:34
  • I've got this field: $form_state['complete form']['field_disavow_file']['und']['0']['#file']->filename ... can I use it for the replaced filename?
    – AgA
    Apr 16 '14 at 6:40
  • Yes you can if that is what it is..
    – Anil Sagar
    Apr 16 '14 at 6:41
  • I mean will it work for all node forms? Is it the standard way?
    – AgA
    Apr 16 '14 at 6:43
  • Yes.. If you see for one content type for other too it's same.. Regarding 'standard way'.. I may prefer going to presave hook .. I believe it's opinion based..
    – Anil Sagar
    Apr 16 '14 at 6:45

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.