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

yelvington’s picture

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...

315redad’s picture

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?

315redad’s picture

how to modify node when use hook node insert? Is that isn't possible?

jaypan’s picture

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.

315redad’s picture

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?

knoboid’s picture

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.

knoboid’s picture

Could you just add the field when you create the node, then save?

global $user;
$node_title = 'Title';
$node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => 'invite_code', 'language' => LANGUAGE_NONE, 'title' => $node_title);
// Set other fields, if necessary.
$random = mt_rand(10000,100000);
$node->field_auto_invite_code[LANGUAGE_NONE][0]['value'] = $random;
node_save($node);
// $node->nid is now available.
miqmago’s picture

<?php
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); // You cannot use it as hook_node_insert is called from node_save => recursive loop
      field_attach_update('node', $node); // Do this instead
      break;
    
   
  }
}
?>
vanvemden’s picture

Thank you, very helpful!

jodathegrey’s picture

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.

"

/**
 * When adding a new node which needs a TINY URL field, then update this programmatically
 * Thanks to David Walsh for the CURL code snippet used!!!
 * Need to use _node_insert so that the Node ID is available
 */
function matrix_node_insert($node) {
	// Had to bring in $base_url or it sometimes comes back as undefined
	global $base_url;
	if($node->type == 'article' && $node->is_new) {
		$mynid = 0;												// have to use this code to get the ID back, for some reason
		$mynid = $node->nid;
		$path = $base_url.'/node/'.$mynid;						// format base URL and then add node
		$tmptiny = get_tiny_url($path);							// get tiny URL for this URL
		$field_info = field_info_field('field_short_uri');		// Get language code (if translatable) for the target node field
		$langcode = $field_info['translatable'] ? $content_langcode : LANGUAGE_NONE;
		$node->field_short_uri[$langcode][0]['value'] = $tmptiny;
		$node->is_new = FALSE;  								// stops duplicate insert error
		$node->llcheck =1; 										// stops recursion
		node_save($node);										// save node changes made
	}
}

function get_tiny_url($url)  {  
	$ch = curl_init();  
	$timeout = 5; 
	curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);  
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
	$data = curl_exec($ch);  
	curl_close($ch);  
	return $data;  
}

"

hyltonmillar’s picture

Thanks for this. Helped me out when requiring the nid on an insert.

RivkiTzipory’s picture

Thanks for this, miqmago !
This row Work for me:
field_attach_update('node', $node); // Do this instead

nitin.k’s picture


function mycustom_node_insert($node) {
  switch ($node->type) {
    case 'invite_code':

      /**
       * Point 1: This code will be called when node is already inserted in the database.
       * Point 2: Do not use field_auto_invite_code['und'][0]['value'].. use field_auto_invite_code[LANGUAGE_NONE][0]['value'].
       * Point 3: Here node should be updated not inserted.
       */
      $random=mt_rand(10000,100000);
      $node->field_auto_invite_code['und'][0]['value']=$random;
      node_save($node);
      break;

      /**
       * Solution
       */
      $random=mt_rand(10000,100000);
      $node = node_load($node->nid);
      $node_wrapper = entity_metadata_wrapper('node', $node);
      $node_wrapper->field_auto_invite_code->set($random);
      $node_wrapper->save();

  }
}