Here is an old story: How to create a node in Drupal programatically. This is my version.
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:
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:
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.
Node object before saving:

Node object after saving:

So, if you want to become familiar with node structure, grab a node and dump it.
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:
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:
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 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:
As the final word, every time you felt stuck, have a look at an existing node structure.
Comments
Post new comment