Dominique De Cooman
Published on Dominique De Cooman (https://dominiquedecooman.com)

Home > Drupal 6 importing taxonomy terms easy mode

Dominique De Cooman
Saturday, August 29, 2009 - 22:58
drupal6 [1]
importing [2]
taxonomy [3]

I just discovered a very handy api to import taxonomy.
It is included with the taxonomy csv module [4] 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($term, TAXONOMY_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.


Source URL: https://dominiquedecooman.com/blog/drupal-6-importing-taxonomy-terms-easy-mode

Links
[1] https://dominiquedecooman.com/blog-topics/drupal6
[2] https://dominiquedecooman.com/blog-topics/importing
[3] https://dominiquedecooman.com/blog-topics/taxonomy
[4] http://drupal.org/project/taxonomy_csv