TransWikia.com

Node is not created, but nid is returned

Drupal Answers Asked by ZioBudda on December 26, 2021

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?

2 Answers

The error in the code shown in the question is that it's setting $node->nid, but not $node->is_new. For node_save(), the node is an existing node that needs to be updated. In fact, the function contains the following code.

// Load the stored entity, if any.
if (!empty($node->nid) && !isset($node->original)) {
  $node->original = entity_load_unchanged('node', $node->nid);
}
field_attach_presave('node', $node);
global $user;

// Determine if we will be inserting a new node.
if (!isset($node->is_new)) {
  $node->is_new = empty($node->nid);
}
// Save the node and node revision.
if ($node->is_new) {

  // For new nodes, save new records for both the node itself and the node
  // revision.
  drupal_write_record('node', $node);
  _node_save_revision($node, $user->uid);
  $op = 'insert';
}
else {
  // For existing nodes, update the node record which matches the value of
  // $node->nid.
  drupal_write_record('node', $node, 'nid');
  
  // …
}

It tries to update a database record that doesn't find, if there aren't nodes with that node ID, and it doesn't save anything. That's why you don't find a node with that node ID, in the table used for the nodes.

Answered by apaderno on December 26, 2021

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);
  }

Answered by inizio on December 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP