Taxonomy Functions (and a Taxonomy Function Gotcha)

I've been working on a custom module recently that periodically imports data from a MS SQL database via (thankfully!) a web service. One interesting chore so far has been setting up the .install file so that the taxonomy tables are pre-populated with the same terms used by the incoming data.

I was suprised at how easy this all was. (Re-reading Chapter 14 of Drupal Pro Development really helped.) You create a properly arranged vocabulary array, then pass it to taxonomy_save_vocabulary. Then, for each term, create an array which is passed to taxonomy_save_term. Very cool!

I did find one, small gotcha, however. According to p.235 of PDD, the nodes key of the $vocabulary argument passed to taxonomy_save_vocabulary is an "array of node types to which this vocabulary applies." And that's not untrue; but it could be a bit more specific. What you actually have to do is create an associative array of node types, where the node type is the key and the value is '1'. This '1' denotes that the node type has been 'selected' -- mimicking, I believe, the behavior of a form (which is the 'normal' way to insert vocabularies). So your code might look something like (keep your eye on that 'nodes' key):

		$vocabulary = array(
			'name'        => 'EPRO topics',
			'description' => 'Broad categories which can describe EDC projects',
			'help'        => '',
			'nodes'       => array ('project' => 1),
			'hierarchy'   => 1,
			'relations'   => 0,
			'tags'        => 0,
			'multiple'    => 1,
			'required'    => 1,
			'weight'      => -10,
			'module'      => 'epro',
		);
		
		taxonomy_save_vocabulary($vocabulary);

Again, I'm something of a newbie here, so let me know if I've got this wrong.