1

I have Drupal 7 site. I am creating node in my module as follows:-

    $newNode = new stdClass();
    $newNode->title = "Hello Node";
    $newNode->type = "product";
    node_object_prepare($newNode); // Sets some defaults.
    $newNode->field_prod_type = 1;
    $newNode->field_prod_cost = 125.00;

    node_submit($newNode); // Prepare node for saving
    node_save($newNode);

If I echo the above newNode I do get the successfull created new node id

echo "<pre>; print_r($newNode); exit();

Issue:-
But when I check in the database, I don't see any new record with the above node details.

Any help highly appreciated.

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Ao what do you see when you go to yoursite/node/[nidofnewnode] ? node creation looks fine, but setting values for the fields looks wrong, try without them: `$newNode->field_prod_type = 1;` `$newNode->field_prod_cost = 125.00;` – justtry Jul 24 '16 at 15:04
  • @justtry. Yes you are correct. Node is accessible ' yoursite/node/[nidofnewnode]', but in the database I don't see the values – Kgn-web Jul 25 '16 at 08:51

1 Answers1

0

You have to use the node_object_prepare function to prepare the node values, as you have done.

But the node will not be published (status = 1) and have no user attached (uid = 0?). And finally, the format of field are always arrays, with 2 dimensions (language and delta). Then your code must be

$newNode->field_prod_type['und'][0] = 1;
$newNode->field_prod_cost['und'][0] = 125.00;

Then if you look into your 'node' table, you must have the record for the returned nid. But you cannot have the records in the fields tables ('field_data_field_prod_type' and 'field_data_field_prod_cost') if you don't format it correctly in array...

Do you have any error message when you save your node ?

TytooF
  • 168
  • 8