Drupal 6 importing taxonomy terms easy mode

Posted by: 
Dominique De Cooman

I just discovered a very handy api to import taxonomy.
It is included with the taxonomy csv module in the file taxonomy_csv.api.inc

I needed to do something very simple import some terms into a vocabulary (no hierarchy, no related terms, just tags..). I already had my csv parser to create my nodes so I did not use the interface of the module. Since I had my terms in an array I just needed to prepare them and import them with this api function :

<?php
  
//prepare
  
$terms = array(
    array(
'name' => $data[20]),
    array(
'name' => $data[21]),
    array(
'name' => $data[22]),
    array(
'name' => $data[23]),
    array(
'name' => $data[24]),
    array(
'name' => $data[27]),
    array(
'name' => $data[28]),
  );

  foreach (
$terms as $term) {
    
//voc id
    
$term['vid'] = ASSOC;
    if (
$term['name']) {
      
//this is the function
      
taxonomy_csv_term_import($termTAXONOMY_CSV_EXISTING_UPDATE_REPLACE);
    }
  }
?>

But this is not it, the modules api can do a lot more. Import vocabularies in total, create hierarchies, related terms, ...
All help is in the files comments.

Add new comment