Drupal 7 - 将分类法插入节点对象 [英] Drupal 7 - Insert taxonomy into node object
问题描述
我相信Drupal 6我会使用这种方法。
$ cat1_tid = taxonomy_get_term_by_name($ data [$ i] ['cat1']);
$ cat2_tid = taxonomy_get_term_by_name($ data [$ i] ['cat2']);
$ cat3_tid = taxonomy_get_term_by_name($ data [$ i] ['cat3']);
$ node-> taxonomy = array($ cat1_tid,$ cat2_tid,$ cat3_tid);
我认为在Drupal 7我会这样做(我的字段名称是目录)
$ node-> taxonomy_catalog ['und'] [0] = array($ term1Obj,$ term2Obj);
taxonomy_get_term_by_name似乎没有返回正确的对象插入节点对象。
如果任何人可以放轻松,赞赏。
谢谢
编辑
解决方案:
//分类学
$ categories = array($ data [$ i] ['cat1'],$ data [$ i] ['cat2'],$ data [$ i] ['cat3']);
foreach($ categories as $ key => $ category){
if($ term = taxonomy_get_term_by_name($ category)){
$ terms_array = array_keys($ term);
$ node-> taxonomy_catalog [LANGUAGE_NONE] [$ key] ['tid'] = $ terms_array ['0'];
}
}
是我最近用来将命令节点导入到站点中的一些快速脏的代码。中间路径,foreach循环根据需要负责创建和分配条款。
$ command = new stdClass;
$ command-> language = LANGUAGE_NONE;
$ command-> uid = 1;
$ command-> type ='drubnub';
$ command-> title = $ line ['0'];
$ command-> body [LANGUAGE_NONE] ['0'] ['value'] = $ line ['1'];
$ command-> url [LANGUAGE_NONE] ['0'] ['value'] = trim($ line ['2']);
$ command->使用[LANGUAGE_NONE] ['0'] ['value'] = $ line ['3'];
$ tags = explode(',',$ line ['4']);
foreach($ tags as $ key => $ tag){
if($ term = taxonomy_get_term_by_name($ tag)){
$ terms_array = array_keys($ term);
$ command-> field_tags [LANGUAGE_NONE] [$ key] ['tid'] = $ terms_array ['0'];
} else {
$ term = new STDClass();
$ term-> name = $ tag;
$ term-> vid = 1;
if(!empty($ term-> name)){
$ test = taxonomy_term_save($ term);
$ term = taxonomy_get_term_by_name($ tag);
foreach($ term as $ term_id){
$ command-> product_tags [LANGUAGE_NONE] [$ key] ['tid'] = $ term_id-> tid;
}
$ command-> field_tags [LANGUAGE_NONE] [$ key] ['tid'] = $ tid;
}
}
}
node_save($ command);
I have a script which successfully creates new nodes. But I'm having trouble setting the taxonomy before saving.
I believe in Drupal 6 I would use this method.
$cat1_tid = taxonomy_get_term_by_name($data[$i]['cat1']);
$cat2_tid = taxonomy_get_term_by_name($data[$i]['cat2']);
$cat3_tid = taxonomy_get_term_by_name($data[$i]['cat3']);
$node->taxonomy = array($cat1_tid, $cat2_tid, $cat3_tid);
I think in Drupal 7 I would do this (my field name is Catalog)
$node->taxonomy_catalog['und'][0] = array($term1Obj, $term2Obj);
taxonomy_get_term_by_name doesn't seem to return the correct object to insert into the node object.
If anyone can shed some light, appreciated.
Thanks
EDIT
Solution:
// Taxonomy
$categories = array($data[$i]['cat1'], $data[$i]['cat2'], $data[$i]['cat3']);
foreach ($categories as $key => $category) {
if ($term = taxonomy_get_term_by_name($category)) {
$terms_array = array_keys($term);
$node->taxonomy_catalog[LANGUAGE_NONE][$key]['tid'] = $terms_array['0'];
}
}
Below is some quick-and-dirty code I used recently to import "command" nodes into a site. Mid-way down, the foreach loop takes care of creating and assigning terms, as needed.
$command = new stdClass;
$command->language = LANGUAGE_NONE;
$command->uid = 1;
$command->type = 'drubnub';
$command->title = $line['0'];
$command->body[LANGUAGE_NONE]['0']['value'] = $line['1'];
$command->url[LANGUAGE_NONE]['0']['value'] = trim($line['2']);
$command->uses[LANGUAGE_NONE]['0']['value'] = $line['3'];
$tags = explode(',', $line['4']);
foreach ($tags as $key => $tag) {
if ($term = taxonomy_get_term_by_name($tag)) {
$terms_array = array_keys($term);
$command->field_tags[LANGUAGE_NONE][$key]['tid'] = $terms_array['0'];
} else {
$term = new STDClass();
$term->name = $tag;
$term->vid = 1;
if (!empty($term->name)) {
$test = taxonomy_term_save($term);
$term = taxonomy_get_term_by_name($tag);
foreach($term as $term_id){
$command->product_tags[LANGUAGE_NONE][$key]['tid'] = $term_id->tid;
}
$command->field_tags[LANGUAGE_NONE][$key]['tid'] = $tid;
}
}
}
node_save($command);
这篇关于Drupal 7 - 将分类法插入节点对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!