Programmatically create nodes: node_save
Drupal's node_save() function lets you create a node of certain type programatically. This is useful when you are importing the data from a backup file like a csv file. To import the data and create nodes using those data you need to fist create the content type of desired type and also have to create all the fields to which the data will be imported to. You have to make sure that while saving data, the format of the data matches that of field type, otherwise you will have a failed import, not to forget the typo while creating the node programatically.
<?php
//Create node
$node = new StdClass();
$node->type = 'game';
$node->title = t('@date-@time', array('@date' => format_date($om__result->date, 'short'), '@time' => format_date($om__result->time, 'custom', "h:i A")));
//Date
$node->field_date = array(
0 => array(
'value' => format_date($om__result->date, 'short'),
),
);
//Location
$node->field_location = array(
0 => array(
'value' => $om__result->location,
),
);
//Home team
$node->field_home = array(
0 => array(
'value' => $om__result->home,
),
);
//Visitor
$node->field_away = array(
0 => array(
'value' => $om__result->visitor,
),
);
//Division
$node->field_divsion = array(
0 => array(
'value' => $om__result->divisoin,
),
);
//Program
$node->field_program = array(
0 => array(
'value' => $om__result->program,
),
);
//Time
$node->field_time = array(
0 => array(
'value' => $om__result->time,
),
);
//Field
$node->field_number = array(
0 => array(
'value' => $om__result->field_nuber,
),
);
node_submit($node);
node_save($node);
drupal_set_message(t('@node has been created', array('@node' => $node->title)));
?> (3 votes)
- 3724 reads
-
Your feedback





Recent comments
20 weeks 6 days ago
39 weeks 3 days ago
41 weeks 1 day ago
50 weeks 4 days ago
1 year 2 weeks ago
1 year 3 weeks ago
1 year 3 weeks ago
1 year 3 weeks ago
1 year 3 weeks ago
1 year 4 weeks ago