2011-12-23 40 views

回答

2

在Drupal 7的字段可以被添加到任何实体/束,以及用于长期参考领域中使用的词汇表上的实体/束水平设置现场级,

因此,您不需要查询特定内容类型的字段设置,而只需查询字段本身的设置。词汇机器名称都存储在settings阵列的allowed_values重点从field_info_field()返回:

$field_name = 'field_name_of_field'; 
$info = field_info_field($field_name); 

$vocab_keys = array(); 
foreach ($info['settings']['allowed_values'] as $item) { 
    $vocab_keys[] = $item['vocabulary']; 
} 

// $vocab_keys now contains an array of all vocabulary machine names allowed on this field 

希望帮助

+0

酷! Thx共享关于'field_info_field'函数=) – 2011-12-23 16:20:24

+0

@ VladStratulat:没问题:)如果你有兴趣,可以在'/ modules/field/field.info.inc'中找到大量有用的字段信息函数 – Clive 2011-12-23 16:24:38

+0

优秀的解决方案,thnx ! – 2011-12-24 20:47:00

0

如果您在额外领域中保留术语,此代码很有用。

/** 
* Get vocabulary ID by term name applied to node 
*/ 
$tid = $node->your_field[$node->language][0]['tid']; 
$term = taxonomy_term_load($tid); 

/* $term now is the following object 
stdClass Object(
    [tid] => 1 
    [vid] => 1 
    [name] => Name of term 
    [description] => Description of term 
    [format] => full_html 
    [weight] => 0 
    [vocabulary_machine_name] => vocabulary 
) */ 

/** 
* Loading vocabularies 
*/ 
$vocabularies = taxonomy_get_vocabularies(); 

/* $vocabularies now is the following array 
Array(
    [1] => stdClass Object(
     [vid] => 1 
     [name] => Forums 
     [machine_name] => forums 
     [description] => Forum navigation vocabulary 
     [hierarchy] => 1 
     [module] => forum 
     [weight] => -10 
    ) 
    [2] => stdClass Object(
     [vid] => 2 
     [name] => Category 
     [machine_name] => category 
     [description] => 
     [hierarchy] => 1 
     [module] => taxonomy 
     [weight] => -9 

    ) 
) */ 

/** 
* Vocabulary searched by you 
*/ 
$vocabulary = $vocabularies[$term->vid]; 

/* $vocabulary now is the following object 
Array(
    [1] => stdClass Object(
     [vid] => 1 
     [name] => Forums 
     [machine_name] => forums 
     [description] => Forum navigation vocabulary 
     [hierarchy] => 1 
     [module] => forum 
     [weight] => -10 
    ) 
) */