2011-02-13 31 views
5

我有一個成功創建新節點的腳本。但是我在保存前設置分類法時遇到了麻煩。Drupal 7 - 將分類插入到節點對象中

我相信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似乎並沒有返回正確的對象插入到節點對象。

如果有人可以擺脫一些光,讚賞。

感謝

編輯

解決方案:

// 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']; 
    } 
} 

回答

4

下面是一些快速和骯髒的代碼,我最近使用的導入 「命令」 節點到一個網站。在中途,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->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); 
+0

謝謝!我更新了顯示我使用的解決方案的帖子,感謝你的榜樣,歡呼! – 2011-02-13 23:40:11

1

在這裏,該代碼在節點創建之前成功地向節點添加了新的術語。

$my_term_name = 'micky'; 
    $term_array = taxonomy_get_term_by_name($my_term_name); 
    if($term_array == array()){ 
     //empty term .. 
     $term->name = $my_term_name; 
     $term->vid = 1; 
    taxonomy_term_save($term); 
    $term_array = taxonomy_get_term_by_name($my_term_name); 
    } 
    //get the first index of the array . 
    foreach ($term_array as $tid => $term_object)break; 
    $node->field_tag['und'][$tid] = (array)$term_object; 
0

也許我的經驗是獨一無二的,但我發現,使用

$term = taxonomy_get_term_by_name($tag) 
$tid = $term->tid;

導致了錯誤。

我發現在保存$ term後,不需要獲取新創建的術語。

$ term對象更新爲包含新的tid。

0

任何使用LANGUAGE_NONE或'und'來改變字段的答案都不是正確的做法,因爲它假定drupal站點是一種語言。編輯字段的正確方法是使用entity_metadata_wrapper。

$node_wrapper = entity_metadata_wrapper('node', $node); 

// If you have Entity API [entity] module installed you can simply. 
$node_wrapper = $node->wrapper(); 

// It is good practice to check the terms in the field before adding 
// a new one to make sure that the term is not already set. 
$term_ids_current = $node_wrapper->taxonomy_catalog->raw(); 
if (!in_array($term_new_id, $term_ids_current)) { 
    $node_wrapper->taxonomy_catalog[] = $term_new_id; 
} 

// To add multiple terms iterate an array or terms ids. 
$term_ids_current = $node_wrapper->taxonomy_catalog->raw(); 
$tern_new_ids = array(1, 2, 3); 
foreach ($term_new_ids as $term_new_id) { 
    if (!in_array($term_new_id, $term_ids_current)) { 
    $node_wrapper->taxonomy_catalog[] = $term_new_id; 
    } 
} 

// To remove a term. 
$term_ids_current = $node_wrapper->taxonomy_catalog->raw(); 
$delta = array_search($term_remove_id, $term_ids_current); 
if (is_int($delta)) { 
    $node_wrapper->taxonomy_catalog->offsetUnset($delta); 
} 

// To replace all terms. 
$term_new_ids = array(1, 2, 3); 
$node_wrapper->taxonomy_catalog->set($term_new_ids); 

// To get all the fully loaded terms in a field. 
$terms = $node_wrapper->taxonomy_catalog->value(); 

// At the end make sure to save it. 
$node_wrapper->save();