Basic data:
- taxonomy name (ex. 'John Doe')
- taxonomy field (ex. field_term)
Conditions:
- we dont have this taxonomy name in our vocabulary
- we have this taxonomy name in our vocabulary
Problem:
- Drupal taxonomy field saves only 'tid' not title
Soliution is simple. We must save not existing taxonomy name before programmaticall node save.
Here is some (maybe not perfect) code:
$term_name = 'John Doe';
$term_array = taxonomy_get_term_by_name($term_name);
if($term_array == array()) { # empty term .. we can save new taxonomy
$term->name = $term_name;
$term->vid = 1;
taxonomy_term_save($term); # here
$term_array = taxonomy_get_term_by_name($term_name); # call agaim, we need a 'tid' for save
foreach ($term_array as $tid => $term_object)
break;
$node->field_term[$node->language][0]['tid'] = $term_object->tid;
} else {
$term_array = taxonomy_get_term_by_name($term_name);
foreach ($term_array as $tid => $term_object)
break;
$node->field_term[$node->language][0]['tid'] = $term_object->tid;
}
Комментариев нет:
Отправить комментарий