2

I would like to use user-submitted data from one form field and add it to anther field, then submit the form to create a node that has my custom data. I'm having a hard time doing this. Any help is appreciated.

  • create your form for create node and change value before saving node. – Vladimir Lezhnin Jul 22 '11 at 15:41
8

This is what the node api hooks are good for.

From the docs:

The node passed validation and is about to be saved. Modules may use this to make changes to the node before it is saved to the database.

You don't specify D6 or D7

In D6

hook_nodeapi() $op == 'presave'

In D7

hook_node_presave()

You can modify values in your implementations of these hooks and they will be saved.

edit

If you need the NID you have to use hook_node_insert, but you will have to save to the DB directly.

  • D7. The problem I'm running into is this: I need to use the node's nid to populate a node reference field (referencing a different entity with the same id #). However, it appears that the nid field isn't populated until the node is saved. Perhaps I need to reload the node once it is saved? – starsinmypockets Jul 22 '11 at 16:23
  • Edited, with solution, but trying to keep IDs in sync is a very tricky way to go. If one gets created but the other doesn't your site will break. – Jeremy French Jul 22 '11 at 16:40
  • Hmm - adding node_save($node) to hook_node_insert seems to cause a PDO exception - duplicate entry – starsinmypockets Jul 22 '11 at 16:52
  • but you will have to save to the DB directly... – Jeremy French Jul 22 '11 at 17:57
  • 1
    OK, not with node_save() but rather db_insert() I'll give this a try thanks for your help. – starsinmypockets Jul 23 '11 at 1:47

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.