2011-11-03 89 views
0

我試圖用PHP構建一個小工具,從我當前的CMS導入內容到Drupal 7,因爲我有大約10k +的文章引入到目前爲止,我已經得到了標題,摘要,正文,作者和發佈日期來通過,但是當涉及類別(標籤)時,我完全困惑。Drupal 7 API +分類標準

我當前的每個類別/標籤都存儲在數據庫表中,每個表都有自己的ID,名稱和說明。我可以把這個出來,每個文章和排序,但我想(字符串,數組等)。

在我進口,我猜我應該做這樣的事情:

$node->field_tags = array(
    'und' => array(
     array(
      'Update', 
      'News', 
      'Report' 
     ) 
    ) 
); 

我也試過:

$node->field_tags = array(
    'Update', 
    'News', 
    'Report' 
); 

但這些也不是逗號分隔的單詞串料不起作用。 Drupal 7 API文檔似乎沒有解釋我發現的任何地方。

什麼是通過發送標籤的格式或什麼是我無法找到的文檔頁面?先謝謝你!

回答

1

Drupal 7中的術語字段與物理分類術語相關,因此您需要爲每個類別創建一個術語,然後將該引用作爲字段的值添加。

此代碼可以幫助:

// Load the tags vocabulary 
$vocab = taxonomy_vocabulary_machine_name_load('tags'); 

$term = new stdClass; 
$term->vid = $vocab->vid; // Attach the vocab id to the new term 
$term->name = 'Category Name'; // Give the term a name 
taxonomy_term_save($term); // Save it 

// Add the tags field 
$node->field_tags = array(
    LANGUAGE_NONE => array(
    'tid' => $term->tid // Relate the field to the new category term using it's tid (term id) 
) 
); 
+0

謝謝您的回覆!如果我想添加多個標籤怎麼辦?他們當然不能都擁有'tid'指數。 – devincrisis

+0

是的,您添加的每個標籤都是分類術語,每個分類術語都需要獨立存在,否則Drupal沒有任何關聯。 – Clive