Creating a node programmatically

I had to import data from an another database as drupal nodes. My node structure contains cck fields, taxonomy terms, cck locations and book outline as well, so it is a quite complete importation example. I am using here the function drupal_execute rather than node_save, because, with node_save, my taxonomy terms were not attached to the gmap markers until I save each node manually ...

 

Here it is:


$node = (object)$node;

// The author (creator) of the node
$form_state['values']['name'] = 'admin';

// Get the data - $data corresponds to a line of my csv file
// The node title
$form_state['values']['title'] = $data[NOM_HABITATION];
// My nodes are group nodes
$form_state['values']['og_description'] = trim ($data[NOM_HABITATION]);
$form_state['values']['uid'] = 1;
$form_state['values']['vid'] = 1;
$form_state['values']['status'] = 1;
// Creation date
$form_state['values']['created'] = strtotime("now");
$form_state['values']['changed']= strtotime("now");
// Some basic node properties
$form_state['values']['comment'] = 0;
$form_state['values']['promote'] = 0;
$form_state['values']['moderate'] = 0;
$form_state['values']['sticky'] = 0;
$form_state['values']['format'] = 3;
// For multi-language content
$form_state['values']['language'] = 'fr';
$form_state['values']['body'] = $data[DESCRIPTION_FR];
// The operation to perform when submitting
$form_state['values']['op'] = t('Save');

// CCK fields
$form_state['values']['field_imm_annee_construction'][0]['value'] = $data[ANNEE_CONSTRUCTION];
$form_state['values']['field_immeuble_logements'][0]['value'] = $data[NB_LOGEMENTS];

// Taxonomy. Attach a/several category(ies)
$node->taxonomy = find_taxonomy_match ($data[CATEGORIE_LOGEMENT]);

// Address. Multiple CCK locations
$node->locations = create_location_node ($data[ADRESSES], $data[CODES_POSTAUX]);

// Set it in the hierarchy
$node->book = set_book_outline ($data[QUARTIER]);

// Save the node object into the database with drupal_execute
drupal_execute('immeuble_node_form', $form_state, (object)$node);

 

The taxonomy array is build like that:

$taxonomy[$value] = taxonomy_get_term ($value);

$value being a taxonomy id (tid).

$node->locations is an array of CCK locations, one location being created as follow:

            $location['street'] = 'street name';
$location['city'] = 'Montreal';
$location['province'] = 'QC';
$location['postal_code'] = 'H3U9G1'
$location['country'] = 'ca';
$location['is_primary'] = 1;
$location['province_name'] = 'Quebec';
$location['country_name'] = 'Canada';

If you have several locations for a node, be careful as I am not sure if you can have multiple primary locations.

 

I created the book outline like this:

$book = array ();
$sql = "SELECT menu_links.mlid, menu_links.plid, book.bid, book.mlid AS parent_mlid from menu_links JOIN book ON book.mlid = menu_links.mlid WHERE link_title='$parent'";
// Run the query
$results = db_query($sql);
// Fetch the results
while ($fields = db_fetch_array($results)) {
// print_r ($fields);
$book['menu_name'] = 'book-toc-'.$fields['bid'];
$book['bid'] = $fields['bid'];
$book['plid'] = $fields['mlid'];
$book['module'] = 'book';
$book['depth'] = 3;
$book['p1'] = $fields['parent_mlid'];
$book['p2'] = $fields['mlid'];
}

return $book;

thanks and notes

Hi,

Thanks for posting this. I knew roughly how to do this, but it saved me some time reading this.

I had an initial problem with some of the node_* function calls, which was easily solved adding module_load_include('inc', 'node', 'node.pages'); to my function.

Also, I assume that you had already populated $node with some relevant information like the $node->type? I had to populate the node before it would add the group correctly.

$node = new stdClass();
$node->type = 'sc_enrollment_network';

but my auto created groups are working nicely now :) so thanks!

 

Bryan Gurneberg

http://www.drupaler.co.za/

I am glad it helped you. And

I am glad it helped you. And yes, I populated the node type before.