我有很少的节点类型,其中一些有一个额外的字段链接到分类词汇表。如果我知道存在该字段的字段名称和节点类型,我如何获得分类词汇表id或名称?
答案 0 :(得分:2)
在Drupal 7中可以将字段添加到任何实体/包中,对于术语引用字段,使用的词汇表在字段级别设置,不在实体/包级别上。
因此,您无需查询特定内容类型的字段设置,只需查询字段本身的设置。词汇表机器名称存储在field_info_field()
返回的allowed_values
数组的settings
键中:
$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
希望有所帮助
答案 1 :(得分: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
)
) */