Drupal:向字汇添加字段
来自站长百科
该代码片段采用一个变量$my_field,并将其作为一个术语插入到一个字汇中。它还会检查具有相同名称的现有术语,还会重新使用它们。你还可以使用自由标记字汇按经计算的字段值来将节点分组。
在使用此代码片段之前,你需要弄清楚获取新术语的字汇vid(即vocabulary ID)。如果你打开需要的字汇编辑页面,vid就是url结尾的数字(mysite.com/admin/content/taxonomy/edit/vocabulary/#)。记录此数字并将其指派到如下代码的$vid之中。你还需要对$my_field进行指派。(将其当作$node_field[0]['value']的指派来处理)
<?php
$vid = Put your vocabulary ID here
$escaped = check_plain($my_field);
$existing_terms = module_invoke('taxonomy', 'get_term_by_name', $escaped);
if (count($existing_terms) > 0) {
foreach ($existing_terms as $existing_term) {
if ($existing_term->vid == $vid) {
$term_id = $existing_term->tid;
}
}
} else { //the term exists
$edit = array('name' => $escaped, 'vid' => $vid);
taxonomy_save_term($edit);
$term_id = $edit['tid'];
}
// associate node and term
$node->taxonomy = array($vid => array($term_id => $term_id));
?>