File
- modules/taxonomy/taxonomy.module, line 196
- Enables the organization of content into categories.
Code
function taxonomy_save_vocabulary(&$edit) {
$edit['nodes'] = empty($edit['nodes']) ? array() : $edit['nodes'];
if (!isset($edit['module'])) {
$edit['module'] = 'taxonomy';
}
if (!empty($edit['vid']) && !empty($edit['name'])) {
drupal_write_record('vocabulary', $edit, 'vid');
db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d", $edit['vid']);
foreach ($edit['nodes'] as $type => $selected) {
db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
}
module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
$status = SAVED_UPDATED;
}
else if (!empty($edit['vid'])) {
$status = taxonomy_del_vocabulary($edit['vid']);
}
else {
drupal_write_record('vocabulary', $edit);
foreach ($edit['nodes'] as $type => $selected) {
db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
}
module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
$status = SAVED_NEW;
}
cache_clear_all();
return $status;
}
Login or
register to post comments
Comments
Use case
I just saw this in forum module and i think it could be an interesting way to explain how this function work :
<?php
$vocabulary = array(
'name' => t('Forums'),
'multiple' => 0,
'required' => 0,
'hierarchy' => 1,
'relations' => 0,
'module' => 'forum',
'weight' => -10,
'nodes' => array('forum' => 1),
);
taxonomy_save_vocabulary($vocabulary);
?>
Nice explanation
http://drupaldeveloper.in/article/programmatically-create-vocabulary
More complete code
That snippet doesn't show all the options, so let's add this one showing all of them:
<?php
$vocab = array(
'name' => t('Categories'),
'description' => t('Categories for your content'),
'help' => t('Help text'),
'nodes' => array('forum' => 1),
'hierarchy' => 1,
'relations' => 0,
'tags' => 0,
'multiple' => 0,
'required' => 0,
'weight' => 0,
);
taxonomy_save_vocabulary($vocab);
?>
Renamed in Drupal 7
For Drupal 7, see http://api.drupal.org/api/function/taxonomy_vocabulary_save/7
Minor Gotcha between taxonomy load and taxonomy save
In the process of updating a taxonomy vocabulary programatically, I discovered a slight gotcha in that I got the vocabulary object happily via taxonomy_vocabulary_load
$vocab = taxonomy_vocabulary_load($vid, TRUE);In this particular case, I wanted to add another node type to the vocabulary.
So I updated the $vocab (yes, you have to make the node type both the key and the value of an assoc array for some reason)
$vocab->nodes['node_type'] = 'node_type';Then, as I went to save the node using taxonomy_save_vocabulary I discovered that taxonomy_save_vocabulary expects an assoc array (not an object). So I had to do this:
$vocab = taxonomy_vocabulary_load($vid, TRUE);
//added the extra node type
$vocab->nodes['node_type'] = 'node_type';
//There might be a better
$vocab_as_array = array(
'vid' => $vocab->vid,
'name' => $vocab->name,
'description' => $vocab->description,
'help' => $vocab->help,
'nodes' => $vocab->nodes,
'hierarchy' => $vocab->hierarchy,
'relations' => $vocab->relations,
'tags' => $vocab->tags,
'multiple' => $vocab->multiple,
);
taxonomy_save_vocabulary($vocab_as_array);
Which seems to work.
Cast to Array
You can likely get away with just casting the returned $vocab object to an array, and save it like:
$vocab->nodes['node_type'] = 'node_type';$vocab_array = (array)$vocab;
taxonomy_save_vocabulary($vocab_array);