How to add an Vocabulary in an install file
This is how you add an Vocabulary in an install file or in a module.
<?php
/**
* Install
*
**/
...
$vocab_count = db_query("SELECT COUNT(vid) AS vid_count FROM {vocabulary}
WHERE name = '%s'", 'A name');
$vocab_count = db_fetch_object($vocab_count);
$vocab_count = $vocab_count->vid_count;
if ($vocab_count == 0){
$vocab = array();
$vocab['name'] = 'A name';
$vocab['description'] = 'A description';
$vocab['help'] = 'Some help';
$vocab['multiple'] = 0;
$vocab['required'] = 1;
$vocab['module'] = 'zimplicit';
$vocab['nodes']['zimplicit_item'] = 1; //Node type machine name
$vocab['tags'] = 0;
$vocab['weight'] = 0;
taxonomy_save_vocabulary($vocab);
variable_set('zimplicit_item_current_vid', $vocab['vid']); //Used on uninstalled
}
...
/**
* Uninstall
*
**/
...
$vocabulary_to_delete = variable_get('zimplicit_item_current_vid', -1);
if ($vocabulary_to_delete >= 0)
taxonomy_del_vocabulary($vocabulary_to_delete);
...
?>
Here is another version where you can install more than one vocabulary.
<?php
function _install_vocabularies() {
$vocab = array();
$vocab[1]['name'] = 'Vocab One';
$vocab[1]['description'] = 'Vocab One...';
$vocab[1]['help'] = 'Vocab One...';
$vocab[1]['multiple'] = 0;
$vocab[1]['required'] = 0;
$vocab[1]['module'] = 'mymodule';
$vocab[1]['nodes']['channel'] = 1; //Node type machine name
$vocab[1]['tags'] = 0;
$vocab[1]['weight'] = 0;
$vocab[2]['name'] = 'Vocab Two';
$vocab[2]['description'] = 'Vocab Two...';
$vocab[2]['help'] = 'Vocab Two...';
$vocab[2]['multiple'] = 0;
$vocab[2]['required'] = 0;
$vocab[2]['module'] = 'mymodule';
$vocab[2]['nodes']['support_ticket'] = 1; //Node type machine name
$vocab[2]['tags'] = 1;
$vocab[2]['weight'] = 1;
$vocab[3]['name'] = 'Vocab Three';
$vocab[3]['description'] = 'Vocab Three...';
$vocab[3]['help'] = 'Vocab Three...';
$vocab[3]['multiple'] = 1;
$vocab[3]['required'] = 0;
$vocab[3]['module'] = 'mymodule';
$vocab[3]['nodes']['instructions'] = 1; //Node type machine name
$vocab[3]['tags'] = 1;
$vocab[3]['weight'] = 2;
foreach ($vocab as $key => $voc) {
$vocab_count = db_query("SELECT COUNT(vid) AS vid_count FROM {vocabulary}
WHERE name = '%s'", $voc['name']);
$vocab_count = db_fetch_object($vocab_count);
$vocab_count = $vocab_count->vid_count;
if ($vocab_count == 0){
taxonomy_save_vocabulary($voc);
$varname = str_replace(" ", "_", mb_strtolower($voc['name']));
variable_set('mymodule_vocab_'.$varname, $voc['vid']);
}else{
drupal_set_message(t("The vocabulary %voc already exits, make sure it can be used.", array('%voc' => $voc['name'])));
}
}
$voc1 = variable_get('mymodule_vocab_channel_type', 1);
$voc2 = variable_get('mymodule_vocab_content_item_queue_status', 2);
$voc3 = variable_get('mymodule_vocab_instruction_type', 3);
$voc4 = variable_get('mymodule_vocab_status', 4);
$sql3 = "REPLACE INTO `term_data` (`vid`, `name`, `description`, `weight`) VALUES
($voc1, 'A-channel', 'A-channel distributes full text and is almost always published first', 0),
($voc1, 'B-channel', 'B-channel handles a teaser text', 2),
($voc1, 'C-channel', 'C-channel handles a standardized message with a link to a source of the news item.', 3),
($voc1, 'Multi-channel', 'Multi-channel distributes content automatically to more than one site via an API ', 1),
($voc2, 'read', 'The news item has been read', 0),
($voc2, 'active', 'Someone is working on this news item', 0),
($voc2, 'closed', 'This news item has been closed', 0),
($voc3, 'begin', '', 0),
($voc3, 'content', '', 0),
($voc3, 'channels', '', 0),
($voc3, 'publish', '', 0),
($voc3, 'footer', '', 0),
($voc3, 'optimize', '', 0),
($voc3, 'dashboard', '', 0),
($voc3, 'add_news_item', '', 0),
($voc3, 'publish_items', '', 0),
($voc4, 'Empty', 'emt', 0),
($voc4, 'Listed', 'lst', 1),
($voc4, 'Viewed', 'viw', 2),
($voc4, 'Assigned', 'asg', 3),
($voc4, 'Content', 'con', 4),
($voc4, 'Footer', 'fot', 5),
($voc4, 'Optimized', 'opt', 7),
($voc4, 'Channels', 'chl', 6),
($voc4, 'Publishing', 'pub', 8),
($voc4, 'Published', 'don', 9);";
db_query($sql3);
}
?>
Take a look at Install Drupal modules programmatically, http://www.zimplicit.se/knowledge/install-drupal-modules-programmatically
Knowledge keywords: