By KhaledBlah on
Hello,
I am seeking a sensible way to determine the node ID for a new node that is about to be posted. What I want to do is to redirect the user to a certain URL after the user has clicked "submit" on the node editing form of a new node. What I have done so far is to query the node table and determine the highest nid of all existing nodes. I then incremented the result and used that ID for the new node. But of course it might happen that two users add nodes at the same time with the first user starting first but submitting later than the second user.
Any ideas?
Regards,
Khaled
Comments
hook_nodeapi
Check out the hook_nodeapi. One of the ops, (possibly 'insert') should suit your needs. The nid should be in $node->nid i think.
edit:
link here
/Anders
A few ideas
Since you specified "after the user has clicked "submit" ", use the Form API and add a "submit handler".
On the other hand, if nodes are created pro grammatically , you should implement hook_nodeapi() http://api.drupal.org/api/function/hook_nodeapi/6
Implement hook_form_alter() to add an additional submit handler http://api.drupal.org/api/function/hook_form_alter/6 . Use either the Form API "#redirect" attribute http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.... or drupal_goto() to redirect your users http://api.drupal.org/api/function/drupal_goto/6
The submit handler is detailed on this page http://drupal.org/node/751826 "Submitting Forms" section.
Thx, apan and Jason! I have
Thx, apan and Jason! I have already implemented both hooks (hook_nodeapi, hook_form_alter). I have written added a submit handler as well but the thing is that I seek a way to find the node id of the new node before it was actually posted. I should mention that I have "normal" content types and such with a multistep form and I would like to use the same method on both types. It's not the redirecting but the new node ID which is a problem for me.
Regards,
Khaled
Please elaborate
Sorry if misread your post. You can also implement a validate handler which is called before the submit handler.
Are you asking how to select the max nid value from the node table? http://api.drupal.org/api/function/db_query/6
@Jason, That's what I am
@Jason, That's what I am doing already (selection of max node and incrementing that result). But I seek a "standard drupal way" that avoids the conflict of concurrent users creating nodes.
Keys generated by the database
Peruse the node.install file. The primary key is generated by the database. I did a lot of transaction processing using clustered J2EE instances and realize that you can't let one single database instance gen your keys. However, a typical Drupal installation isn't clustered.
Drupal performs cron process with the semaphore table. However, that's not going to catch the type of race condition you are implying.
Maybe someone else should answer this. To my knowledge, php is single threaded. You neither have the control or issues of a mutli-threaded environment.
Sorry, that's the best I can answer :)
Can you give a little more
Can you give a little more data on what you are trying to accomplish? With specific data it will be easier to give recommendations on which hooks and functions to use, where to put them, and how you will get the NID from that.
Contact me to contract me for D7 -> D10/11 migrations.
Here's the SQL statement for
Here's the SQL statement for what I am currently doing. It determines the max. node ID currently used and then I increment it by one.
db_result(db_query_range("SELECT nid FROM {node} ORDER BY nid DESC", 0, 1));I am seeking for a better way because that might cause problems with nodes being created at the same time.
Yes, but why are you doing
Yes, but why are you doing this. Give us as much possible information surrounding your goal, reasoning, and thoughts as you can.
Contact me to contract me for D7 -> D10/11 migrations.
Please read my first post in
Please read my first post in this topic again.
The answer to the question in
The answer to the question in your first post was given in the second post. Seeing as that didn't seem to be what you wanted, you haven't given enough information to be able to help you.
Contact me to contract me for D7 -> D10/11 migrations.
implement hook_nodeapi and change the Drupal destination
May need code to check whether or not it was a form submission or a programmatic node_save in order to avoid redirecting superfluously.
If you find a better way, I'd love to hear it.