0

I have this code in my module.

  $node = new stdClass();
  $node->title = "YOUR TITLE";
  $node->type = "page";
  node_object_prepare($node);

  $node->language = 'it';
  $node->uid = 1; 
  $node->status = 1;
  $node->promote = 0;
  $node->comment = 1;

  $node = node_submit($node);
  node_save($node);

  return "Nodo 222: " . $node->nid;

When I execute this code, it returns "Nodo 222: XXX," where XXX is an integer. If I execute it another time, I get XXX+1. When I try to load node/XXX or node/XXX+1, Drupal give me a Page not found error.

I have seen the "node" table in MySQL (via PHPMyAdmin), and nids XXX and XXX+1 are not present. When I create a page via node/add/page, the new nid is XXX+2 and it is saved in the "node" table too.

I have some modules installed (commerce, rules, logintoboggan and others), but this is the first time that I see this strange behavior.

So, where is my error?

  • Have you tried $node->is_new = TRUE;? Is there anything in the logs? – Triskelion Jul 29 '13 at 21:37
  • I dont see any issues with your code, try with $node->body['it'][0]['value'] = 'Body goes here'; bit.ly/13iisqB – niksmac Jul 30 '13 at 2:22
0

Hope this will help you,

In Drupal 7, The $node object you pass to node_save() wouldn't get it's nid member populated unless the node has been successfully saved to the node table in the database.

The auto-increment ID actually comes from the database table so if the $node object has a nid, you can pretty much guarantee the record has been successfully inserted into the table.

Im just guessing check for "node" table paging.

Clear drupal cache and check again.

I have used the below code and it is working fine for me, you can refer this.

  $node = new stdClass();
  $node->type = "page";
  node_object_prepare($node);
  $node->title = 'test node';
  $node->language = 'it';
  // Set node body details
  $node->body[LANGUAGE_NONE]['0']['value'] = 'test body';
  $node->body[LANGUAGE_NONE][0]['summary'] = text_summary('test body');
  $node->body[LANGUAGE_NONE][0]['format'] = 'filtered_html';
  //.. other fields goes here
  $node->uid = 1;
  $node->status = 1;
  $node->promote = 0;
  $node->comment = 1;
  if ($node = node_submit($node)) {
    node_save($node);
    drupal_set_message('NODE : ' .$node->nid);
  }

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.