By 315redad on
function mycustom_node_insert($node) {
switch ($node->type) {
case 'invite_code':
$random=mt_rand(10000,100000);
$node->field_auto_invite_code['und'][0]['value']=$random;
node_save($node);
break;
}
}
this will get error,not work,
when delete 'node_save($node)',
it will work,
but the $node field 'field_auto_invite_code' has no change. it does not affect the $node,
how to modify the $node in hook_node_insert?
Comments
Presave
By the time hook_node_insert is called, the transaction is already under way. I believe you need to be looking for this:
https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hoo...
Thank you for your help,
Thank you for your help, yelvinton.
i know this way, it will work,
But i want to get the $node nid for uniqid use,
and don't generate node by submit form, only programmatically.
so, how can i do that?
how to modify node when use
how to modify node when use hook node insert? Is that isn't possible?
No. You do it in hook_node
No. You do it in hook_node_presave(). If you try to do it in hook_node_insert() you will create a loop, as calling node_save() will loop the entire process.
Contact me to contract me for D7 -> D10/11 migrations.
I want to get the node nid
I want to get the node nid ,so i can use it to save as a uniqid custom field,
how to do it through a drupal way not by form submit?
hook_node_insert($node)
hook_node_insert($node) provides the node object with the nid asigned.Edit:
My mistake. You've already explored that.
But can't see why you need the node id in order to set the field in hook_node_presave(), as Jaypan suggests.
Could you just add the field
Could you just add the field when you create the node, then save?
<?phpfunction mycustom_node
Thank you, very helpful!
Thank you, very helpful!
Other options calling "node_save" in "_node_insert"
Hi, I've just been working through a similar problem, to programmatically set a 'TINY URL' in a node field, almost catch22 where you need the full URL (including node id) to get the tiny url, so I put code into the _node_insert where I could get the node ID and then update it... but I needed to set a couple of params before calling node_save... see $node->is_new and $node->llcheck below: Thought I'd post this up here in case it was useful for someone else.
"
"
Thanks for this. Helped me
Thanks for this. Helped me out when requiring the nid on an insert.
Thanks for this, miqmago !
Thanks for this, miqmago !
This row Work for me:
field_attach_update('node', $node); // Do this instead
Facts about the above code