5

新しいノードを正常に作成するスクリプトがあります。しかし、保存する前に分類法を設定するのに問題があります。

私はDrupal6を信じています。私はこの方法を使用します。

$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

4 に答える 4

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);
于 2011-02-13T20:08:33.657 に答える
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;
于 2011-08-18T14:35:23.943 に答える
0

おそらく私の経験はユニークですが、

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

エラーが発生しました。

$ termを保存した後は、新しく作成した用語をフェッチする必要がないことがわかりました。

$ termオブジェクトが更新され、新しいtidが含まれます。

于 2012-07-15T15:47:07.623 に答える
0

LANGUAGE_NONEまたは'und'を使用してフィールドを変更する回答は、drupalサイトが1つの言語であると想定しているため、適切な方法ではありません。フィールドを編集する適切な方法は、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();
于 2017-05-09T22:48:37.747 に答える