让我们看一个基于模块的词汇表的例子。第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;
}
老葛的Drupal培训班 Think in Drupal
评论
勘误
if (empty($vid) || taxonomy_vocabulary_load($vid) === FALSE)
这个我不确定,我对PHP的语法不熟悉,is_null()
这个我不确定,我对PHP的语法不熟悉,is_null() 和 ===FALSE是不是有区别
确实是该判断
确实是该判断 ===false;
taxonomy_vocabulary_load($vid)的返回值完全有可能是:FALSE.
但这时is_null(FLASE)返回false,这肯定与语义是有冲突的。
官方勘误:
官方勘误: http://www.drupalbook.com/errata2?page=6 Page 337