Last updated September 10, 2010. Created on September 10, 2010.
Edited by snufkin. Log in to edit this page.

Importing vocabulary and terms together

Function used: install_taxonomy_import().

The goal is to define a complete taxonomy (vocabulary and the relevant terms) in one big associative array. Due to the nature of the term and vocabulary import functions this might seem redundant at first, but individual vocabulary and term creation is covered later on.

The example array is quite self-explanatory:

<?php
$taxonomy
= array(
 
0 => array(
   
'name' => 'vocab name',
   
'content_types' => array(),
   
'properties' => array(),
   
'terms' => array(
     
0 => array(
       
'name' => 'Term Name',
       
'description' => '',
       
'properties' => array(),
      )
    ),
  ),
);

install_taxonomy_import($taxonomy);
?>

It is important to note that even though description and properties are empty here, they still must be set. Otherwise we will encounter an "unsupported operand types in taxonomy.module" error, which is a sign that, you are probably not using the correct array structure. Once again, term description and properties must not be omitted, even if empty!

Creating vocabularies

Function used: install_taxonomy_add_vocabulary()

When creating a taxonomy step-by-step this should be done first, because in order to add terms the parent vocabulary must be specified. The install_taxonomy_add_vocabulary() expects three arguments: name of the vocabulary, an array of content types it will be applied to and an array of properties. Properties have a set of default options, which are the same as if you would create them through the UI. After calling the function the new vocabulary ID is returned.

Example to create a required, free tagging vocabulary for pages and stories, called 'tags':

<?php
$types
= array('story' => 1, 'page' => 1);
$vocab = array(
 
'tags' => TRUE,
 
'required' => TRUE,
);

$vid = install_taxonomy_add_vocabulary('tags', $types, $vocab);
?>

Note the way I defined the content types. This is unfortunately a counterintuitive syntax, but is necessary because when the vocabulary is saved via taxonomy_save_vocabulary() it looks at the value of the key in the types array instead of a value (if I had array('story', 'page') the keys would be 0 and 1).

Creating terms

Function used: install_taxonomy_add_term().

Once the vocabulary is created we can generate a few terms (essential for any node import later on). install_taxonomy_add_term() is very similar to the previous function, only additional parameter is the vocabulary id we want to assign the terms to. In the example we will define a number of taxonomy terms in a nice array, then generate them one by one, through a for loop. Note: I could generate the terms one by one after defining, but such a definition is more elegant, because it is easier to change later on. Also, I could have defined a separate $properties array, but it would serve no purpose as the $terms array is already structured in a way install_taxonomy_add_term() can process. For possible properties please see the function reference linked above.

Example (note: $vid comes from the previous example):

<?php
$terms
= array();
$terms[] = array(
 
'name' => 'Socks',
 
'description' => 'User stories detailing their experience with socks.',
);
$terms[] = array(
 
'name' => 'Whales and fishing',
 
'description' => 'Stories related to whales and fishing.',
);

foreach (
$terms as $term) {
 
$tid = install_taxonomy_add_term($vid, $term['name'], $term['description'], $term);
}
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.