让我们看一个基于模块的词汇表的例子。第
3方的相册模块(包含在图片模块;参看http://drupal.org/project/image)使用分类来管理不同的相册。它通过代码创建了它的词汇表,如下面的例子所示,并且通过将$vocabulary数组的module键设为该模块的名字(不带.module)来锁定该词汇表的所有权。
/**
* Returns (and possibly creates) a new vocabulary for Image galleries.
*/
function _image_gallery_get_vid() {
$vid = variable_get('image_gallery_nav_vocabulary', '');
if (empty($vid) || is_null(taxonomy_vocabulary_load($vid))) {
// Check to see if an image gallery vocabulary exists.
$vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE
module='image_gallery'"));
if (!$vid) {
$vocabulary = array(
'name' => t('Image Galleries'),
'multiple' => '0',
'required' => '0',
'hierarchy' => '1',
'relations' => '0',
'module' => 'image_gallery',
'nodes' => array(
'image' => 1
)
);
taxonomy_save_vocabulary($vocabulary);
$vid = $vocabulary['vid'];
}
variable_set('image_gallery_nav_vocabulary', $vid);
}
return $vid;
}