2011-12-23 44 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 
    ) 
) */