Drupal 7 - Insert taxonomy into node object

2019-03-30 01:02发布

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

4条回答
smile是对你的礼貌
2楼-- · 2019-03-30 01:49

Perhaps my experience is unique, but I found that using

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

caused an error.

I found that after $term is saved, there is no need to fetch the newly created term.

The $term object is updated to include the new tid.

查看更多
Lonely孤独者°
3楼-- · 2019-03-30 02:01

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);
查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-30 02:01

Here you are, this code successfully add a new term to the node before the node is created.

$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;
查看更多
Deceive 欺骗
5楼-- · 2019-03-30 02:01

Any answer that use LANGUAGE_NONE or 'und' to alter a field is not the proper way of doing it as it assumes that the drupal site is one language. The proper way to edit a field is to use 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();
查看更多
登录 后发表回答