1

I am working on a Drupal 7 custom module and would like to update a field on a node after hitting save. Based on some post that I have seen this should be possible.

As it is now I have the $node object is a parameter to the node_submit hook after hitting save.

However, I am not able to update my field on this node object. Any pointers or examples of how to do this would be greatly appreciated.

Thanks, Derek

1 Answer 1

1

Depending on your exact usage, you'd want to use either

1) hook_node_presave - Called on any node being saved (inserted or updated). see here

2) Attach a submit handler to the form: Allows to change the form's values before the submit is completed.

4
  • Amir, thanks so much for you replay. I tried the hook_node_presave approach which seems promising. However, I am noticing that hook_node_presave seems to occur after hook_node_submit. And I am working with some variables in hook_node_submit which I trying to use to update the field value with.
    – geo derek
    Apr 11, 2015 at 3:33
  • Again, usage depends on what you need. hook_node_presave occurs just before the node is being saved to the database, regardless of context which will allow you to globally change things, but without the form's context. Using a submit handler will let you change the submitted values before anything else occurs - with all the values inputted in the form's field.
    – Amir Arbel
    Apr 11, 2015 at 9:45
  • I like the form_alter approach and am trying that as a submit handler. However, I am trying read the path to a file that is uploaded within this handler. This function fires before save is hit. This may really be for a different topic but do you know if it possible to get the path to a file upload within the form_alter? Thanks.
    – geo derek
    Apr 12, 2015 at 14:48
  • Amir, I was able to get this working with hook_file_insert. Thanks again for your original advice as it did point me in the right direction.
    – geo derek
    Apr 12, 2015 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.