This snippet takes a variable $my_field and inserts it as a term in a vocabulary. It also checks for existing terms with the same name and will reuse those terms. You can do this with a free tagging vocabulary to group nodes by your calculated field values or just about anything.

Before using this snippet you need to figure out the vid (vocabulary ID) of the vocabulary that will get the new term. If you go to the edit page for the vocabulary you want the vid is the number at the end of the url (mysite.com/admin/content/taxonomy/edit/vocabulary/#). Take that number and assign it to $vid in the code below. You will also need to assign $my_field to whatever you want the term to be (treat it like your $node_field[0]['value'] assignment).

	$vid = Put your vocabulary ID here

	$escaped = check_plain($my_field);
	$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 = array($vid => array($term_id => $term_id));

Comments

bobblebob’s picture

For me this adds the term to the vocabulary but doesn't associate node and term. Does this snippet work for D6?
What should the vocabulary settings be for this to work?

tagawa’s picture

The same thing for me.
The term is added to the vocabulary but not assigned to the node. A sort-of workaround is to use the taxonomy_node_save function:

taxonomy_node_save($node, $node->taxonomy)

but now I get a "duplicate error" message if I subsequently re-save the node.
Any experts know how to prevent this?

eskilh’s picture

Adding

$tid = '3';

and replacing:
$edit = array('name' => $escaped, 'vid' => $vid);
with
$edit = array('name' => $escaped, 'vid' => $vid, "parent"=>$tid );

Allows you to save the term as a child of the term with tid=3.

swopit’s picture

How could I do this the other way round and insert several vocabularies in a CCK textfield (dropdown) via php code? I simply want to select the specific vocabularies by VID and display them with their real name in a list, below each other, to make them avaliabel for the drop-down-menu in CCK.
The taxonomy module is not what I want, thus my question...

Patrick

HolyBoY’s picture

hi all

i use this code in computed field but code is not working. not add to vocabulary as a term and not associate node and term.
what should i do? help please

here is my code:
$vid = 1

$escaped = check_plain($node->field_ee[0]['value']);
$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 = array($vid => array($term_id => $term_id));

$node_field[0]['value'] = "www";

mrenoch’s picture

I had a pretty hard time tracking out how to do this successfully in Drupal7. This is currently working for me right now. I am actually updating a field that is outside of my computed field (I don't know if that is completely cool, but its working for me). So, my computed field is a string, but in my computed field code I am updating a term reference that is on the same node.

This isn't the complete operation - just the snippet that I using to manipulate the term reference field.

// set the vocabulary id for the term reference you are working with
$vid =  <NN>; 
$tid = NULL;

// make sure you find the term id for this vocab 
$terms = taxonomy_get_term_by_name($<MY_TERM>);
foreach($terms as $term) {
      if($term->vid == $vid) {
          $tid = $term->tid;
          break; 
       } 
} 
// if the term doesn't exist yet, create a new one, and then get its tid
if (!$tid) {
       $new_term = array(
         'vid' => $vid,
         'name' => $<MY_TERM>,
       );
       $new_term = (object) $new_term;
       taxonomy_term_save($new_term);
       $tid = $new_term->tid;
}
    
// only update the term reference field if its not already set  
if (empty($entity->field_<MY_TERM_REREFENCE_FIELD>[LANGUAGE_NONE])) {
         $entity->field_<MY_TERM_REREFENCE_FIELD>[LANGUAGE_NONE][]['tid'] = $tid;
}

pegahmajma’s picture

that's great.this work for me.thanks for sharing

Pegah Majma