Parent/Child Relationships for Terms by Computed Field

kristyb2008 - October 4, 2009 - 01:27
Project:Computed Field
Version:6.x-1.0-beta3
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

I posted this here..
http://drupal.org/node/595336

however I'm thinking this would probably be a better place for my question:

I would like to auto assign a term for cck location fields via computed fields.

Therefore, Country would be a parent term and Province/State would be a child term which would be assigned to Vocabulary "Location" when the node is saved.

Here is the code I've used for assigning general cck values as terms, however I'm not too sure how I could save two terms to one vocabulary at a time, while maintaining parent/child relationship.

$position = $node->field_profile_position[0]['value'];

$vid = 10;

$escaped = check_plain($position);
$existing_terms = module_invoke('taxonomy', 'get_term_by_name', $escaped);
if (count($existing_terms) > 0) {
foreach ($existing_terms as $existing_term) {
if ($existing_term->vid == $vid) {
$term_id = $existing_term->tid;
}
}
} else { //the term exists
$edit = array('name' => $escaped, 'vid' => $vid);
taxonomy_save_term($edit);
$term_id = $edit['tid'];
}

// associate node and term
$node->taxonomy[] = $term_id;
if(empty($node->build_type)) taxonomy_node_save($node, $node->taxonomy);

 
 

Drupal is a registered trademark of Dries Buytaert.