How to create a new node in Drupal programatically

Here is an old story: How to create a node in Drupal programatically. This is my version.

Basics

First, if you have a general knowledge of a node structure, it would be much easier for you to create nodes by code. If you do not have such knowledge, don't worry; I will show you in this article.

First of all, a node is a container, containing data of your Story or Page etc. In code, it is represented as a PHP Object. All we do is creating an object and putting our data in it:

  1. global $user;
  2.  
  3. // Create new object.
  4. $node = new stdClass();
  5.  
  6. // Set desired data and metadata as members in object.
  7. $node->type = 'page';
  8. $node->uid = $user->uid;
  9. $node->title = 'node title';
  10. $node->body = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit...';
  11.  
  12. // Save the node
  13. node_save($node);

Note that if you don't set some values -Status or Promote to front page for example, they will be initialized by default values in Content type settings. If you want to overrode those values, set them as well:

  1. global $user;
  2.  
  3. $node = new stdClass();
  4. $node->type = 'page';
  5. $node->uid = $user->uid;
  6. $node->title = 'node title';
  7. $node->body = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit...';
  8.  
  9. $node->status = 0; // Not published
  10. $node->promote = 1; // Promote to front page
  11. $node->sticky = 1; // Sticky on top of list
  12.  
  13. node_save($node);

Here is a good place to have a look at a node structure. When you pass your node to function node_save(), it adds the necessary values to it and then save it. This function accepts your node as a refrenced variable, therefore after saving the node, you can have a look at it and see the complete structure of your new node using functions print_f() or dpm(). function dpm() is one of many useful utility functions in module Devel. If you have not installed Devel yet, I strongly recommend that do it right now! It is a must for every Drupal developer.

  1. // Dump before saving:
  2. dpm($node);
  3.  
  4. node_save($node);
  5.  
  6. // Dump after saving:
  7. dpm($node);

Node object before saving:

Node object after saving:

So, if you want to become familiar with node structure, grab a node and dump it.

Advanced

All I said by now was about basic content types -like page and story- which contain minimum necessary fields that a node have. But what about complex content types like Pole in pole module or content types that are created or altered using CCK module?

There is no precise rule for how extra fields are stored in nodes of different types. But that's not a problem. All you need to do is having a look at node structure of an existing node. Do following steps:

  • create a node of a certain type using Drupal interface.
  • Fill in all fields.
  • If a field accepts multiple values, enter two or three values in it.
  • Save the content.
  • Dump the node and have a look at the structure.

  1. // Load the node.
  2. $node = node_load(123);
  3.  
  4. dpm($node);

Code snippet above, loads a node that has nid of 123 and dumps the node structure.
Following image shows a small part of a node with few CCK fields:

I cannot get into details of every node type in this article; but I will explain the structure of content types that are created with CCK module a bit. First, I suggest you to check an existing node of the desired type.

For setting value of CCK fields, first you need to know the machine-readable name of the CCK field. You can find it in the Content management > Content types > You Content Type > Manage fields page, in the Name column of table.
All CCK fields consist of zero-based numeric indexed array containing all the field instances -single or multiple values.
For example, setting value for a single integer CCK field is something like this:

  1. $node->field_integer = array(
  2.   0 => array(
  3.     'value' => 256
  4.   ),
  5. );
  6.  
  7. // Or simply:
  8. $node->field_integer[]['value'] = 256;

First index is index of value and second index -which always is string- is name of the element. second index varies in field types. here are some common values:

  • For fields of type Integer, Float or String, it is 'value'.
  • For fields of type Node reference, it is 'nid'.
  • For fields of type User reference, it is 'uid'

For compound CCK fields which each value is compounded of two or more elements, each value has two or more string indices which point to the relevant element. For example, each Money CCK field value contains two string indexes: One is amount, and the other is currency; or the Date CCK field is contains five different elements.

Here are few examples:

  1. // A CCK field of type User reference.
  2. $node->field_user = array(
  3.   0 => array('uid' => 12),
  4. );
  5.  
  6. // A CCK field of type Node reference with multiple values.
  7. $node->field_node = array(
  8.   0 => array('nid' => 12),
  9.   1 => array('nid' => 34),
  10.   2 => array('nid' => 56),
  11. );
  12.  
  13. // A CCK field of type Money.
  14. $node->field_money = array(
  15.   0 => array('amount' => 5000, 'currency' => 'USD'),
  16. );

As the final word, every time you felt stuck, have a look at an existing node structure.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.