i need to add in the fly a taxonomy to a book. I've put an extra field
in my book (a cck node) in case the user want to add a new taxonomy
without going to the form taxonomy.

I've a custom module to handle this:

function source_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'submit':
if ($node->type == 'content_libro') {
echo "Nuevo Autor: " . $node->field_nuevo_autor[0]["value"];
}
break;
case 'insert':
break;
case 'update':
if ($node->type == 'content_libro') {
echo "Nuevo Autor: " . $node->field_nuevo_autor[0]["value"];
}
break;
case 'view':
break;
}
}

Now i need to create the taxonomy and then to put the book node in this
taxonomy. I suppose it can be done with taxonomy_nodeapi o something
similar but i'm newby in Drupal.

thanks in advance,

Comments

carlitus’s picture

Within case submit:

if ($node->type == 'content_libro') {
				if(trim($node->field_nueva_editorial[0]["value"]) != "")
				{
					$edit = array();
					$vid = 6; // Vocabulary number, only one value
					$edit["name"] = $node->field_nueva_editorial[0]["value"];
					$edit["vid"] = $vid;
					$edit = taxonomy_save_term($edit);
					
					//echo "<p>" . $node->field_nueva_editorial[0]["value"] . "</p>";
					
					$editorial = $node->field_nueva_editorial[0]["value"];
					$term = taxonomy_get_term_by_name("$editorial");
					$node->taxonomy[$vid] = $term[0]->tid;
				}
				$node->field_nueva_editorial[0]["value"] = NULL;
				
				if(trim($node->field_nuevo_autor[0]["value"]) != "")
				{
					$edit = array();
					$vid = 1; // Vocabulary number, multiple values
					$edit["name"] = $node->field_nuevo_autor[0]["value"];
					$edit["vid"] = $vid;
					$edit = taxonomy_save_term($edit);
					$term = taxonomy_get_term_by_name($node->field_nuevo_autor[0]["value"]);
					$node->taxonomy[$vid][$term[0]->tid] = $term[0]->tid;
				}
				$node->field_nuevo_autor[0]["value"] = NULL;
			}