• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

Userpoints Meter

0 Points /1000 Points

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

5
Your rating: None Average: 5 (3 votes)
Syndicate content

Recent comments