By modul on
I have a function "my_module_node_submit($node, $form, &$form_state)" in my_module.module, which is working just fine after the user has clicked the Submit button in Edit or Add mode.
In that function, I need the node id (the "$nid").
When Editing an existing node, that's trivial:
$mynid = $form['#node']->nid;
But when I'm in "add/new" mode, i.e. when I'm adding a new node, how do I get the "future" nid of my node about to be saved? I suppose maybe something with the node_save() function? But what? How can I "hook" into which Drupal function to retrieve the future nid of my new node? Does anyone have a simple example of working code, a function I could call from my Submit function?
Comments
use hook_node_insert
use hook_node_insert
Could you be a bit more
Could you be a bit more specific? How should I call hook_node_insert? Where? Which parameters?
The idea is that I have the New Node Nid available in my_module_form_submit. As I said, for an Edited node, there's no problem, but for a New node, there definitely is. Actually, I am trying to change something in the body Before the new node is being saved. But I will only know the new nid After it is being saved, and that's too late. So, how to figure out the nid-to-be-used?
Use hook_node_presave
So you want to change something in the body at the time of node save(insert/update). Use hook_node_presave. You do not need to know the nid.
I hope you have knowledge of hooks. the argument is $node and it got called before node insert/update, so you have an option to change node body before node save. Hope this will help.
You can have lots of example in the community how to use node_presave hook.
Would it be a bad idea to use
Would it be a bad idea to use a simple MySQL query - SELECT MAX(nid) AS mynid FROM {node} - and simply add 1 to find the next available nid, which my new node will then be using?? The risk is that someone would successfully insert another new node in the few milliseconds between my query and my own insert, but in a limited group of people adding nodes I would say that this risk is very limited. Or is this approach asking for trouble?
---
Why do you need the nid before you save the new node? You can still have the user save the node, then get the NID and update the node if you're trying to add the nid to it somewhere.
Maybe I'm asking too much,
Maybe I'm asking too much, but could you give me a few lines of working code on that? I'm in my my_module_node_submit function, and there I would like to do something with the nid of the newly saved node. How could I manage to do that? How to retrieve the new nid? I would need to call some sort of hook_post_save function (which, sadly, doesn't seem to exist), I suppose, but I don't have a clue on how to set this up. And grabbing the new nid is important. If it's possible with hook_pre_save, as was hinted at higher up in this posting, fine with me, but how??
_
Why do you need the nid in a submit function? By definition, you don't have the nid in _submit.
If you use hook_node_insert you can act immediately after the node has been created (and you will have access to the nid at that point if you really do need it).
As implied by 'presave' in hook_node_presave the nid is not available there for newly created nodes; although existing nodes being updated will have a nid at that point.
Thanks, WorldFallz, you put
Thanks, WorldFallz, you put me on the right track. In my my_module.module, I added this function, and it gives me the nid of NEW nodes, just what I was looking for. Instead of desperately wanting to hunt for the new nid in my_module_node_submit, which was driving me nuts, I added a simple new function my_module_node_insert, and bingo:
It's a nice day.
_
exactly how it's supposed to be used! excellent.