You are here

Drupal专业开发指南 第14章 创建一个基于模块的词汇表

g089h515r806 的头像
Submitted by g089h515r806 on 星期五, 2009-08-21 15:46

 

让我们看一个基于模块的词汇表的例子。第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

Drupal版本:

评论

thomasfan 的头像

if (empty($vid) || taxonomy_vocabulary_load($vid) === FALSE)

pangjanne 的头像

确实是该判断 ===false;

taxonomy_vocabulary_load($vid)的返回值完全有可能是:FALSE.

但这时is_null(FLASE)返回false,这肯定与语义是有冲突的。