Posted by gallamine on August 27, 2010 at 3:19am
I'm trying to programatically create, and add a term to a node. Here's what I've got so far:
function mymodule_nodeapi(&$node, $op, $teaser, $page) {
if($node->type == 'robot_club') {
switch ($op) {
case 'insert':
// create new taxonomy term
$termname = $node->title;
$term = array(
'vid' => 6, // Voacabulary ID - robot_clubs
'name' => $termname // Term Name
);
if (taxonomy_save_term($term)) {
$newterm = taxonomy_get_term_by_name($termname);
$node->taxonomy = $newterm;
} else {
drupal_set_message(t('Awwww snap! Something goofed up with adding the proper taxonomy terms to this club!', 'error'));
}
...The term is created fine, but it isn't added to the node. Previously I tried taxonomy_node_save($node, $newterm), but that didn't work either. Help?

Comments
I think part of the problem
I think part of the problem you're running into is taxonomy_get_term_by_name() returns an array.
Try:
$node->taxonomy[] = array_pop($newterm);Try: <?php$node->taxonomy =
Try:
<?php$node->taxonomy = $newterm;
node_save($node);
?>
Not sure what exactly you are
Not sure what exactly you are trying to accomplish in the end with this node/taxonomy relationship, but there are two modules you might want to look into (if you haven't already):
Node Auto Term http://drupal.org/project/nat
Taxonomy Node http://drupal.org/project/taxonomy_node
Ok, so
Ok, so calling:
$node->taxonomy = $newterm;node_save($node);
works for newly created nodes, but when I try and add another term to an existing node. It won't work. I've tried the following:
$user_profile->taxonomy[] = $clubterm;node_save($user_profile);
and
$user_profile->taxonomy[$clubterm->tid] = $clubterm;node_save($user_profile);
Neither of them will add the term object to the node $user_profile.
Confused.
Any alternative solution
I already had 16 thousand nodes,for all that node i want to assign terms id's
I'm an idiot. Typo. The
I'm an idiot.
Typo.
The following works fine:
$user_profile_node->taxonomy[] = $clubterm;node_save($user_profile_node);
Everyone can move along now ...
I am also trying to do
I am also trying to do exactly the same thing. but for Drupal 7. Where does this $node->taxonomy variable come from.
When inserting nodes this variable does not exist, is this a cck field that you have set and are overriding?
how do it with ubercart
I try to do that with creating a node in type 'product' and i try to apply this to add a taxonomy term but it doesn't work.
can someone help me pls?
ubercart
Are you trying to add another taxonomy field to the product node? Ubercart product pages are set up with a taxonomy field.
If i remember right, Ubercart creates a taxonomy vocabulary for your catalog (that is editable). You add terms to the vocabulary to subdivide your catalog (product line) e.g.;
catalog
- clothes
- - men's
- - -shirts
- - -pants
- - women's
- - -shirts
- - -pants
- shoes
I think you have to go in and set up the terms before being able to choose them in the taxonomy field on the product page. Although the fields can be changed at admin/content/node-type/product/fields, my product page fields are "Name, Description, Taxonomy (catalog), Image, Product information, Shipping, Menu settings, Description, Meta tags, Revision information, Authoring information, Publishing options, Path settings, File attachments". After setting up the taxonomy terms, they should be available on the product page in the taxonomy field.
However, doing all of that is different than the rest of what this thread is talking about.
is it for D7?
the function mymodule_nodeapi nay work on D7?
hook_nodeapi was split in D7
See http://drupal.org/update/modules/6/7#remove_op
Ken Winters
Separate hooks (mostly)based on old operations
As pointed out above, it was split into separate hooks. Generally, the quick way to convert nodeapi code to 7.0 is to just leave the hook_nodeapi (for backwards compatibility) and make each "operation" (e.g. insert/update) call the new hooks. Pretty easy/quick to just copy the existing code to the new functions.
FWIW - The reason for this was to improve performance since a lot of modules implement nodeapi for creation/update actions only. Makes for fewer module hooks when just getting the node content.
How to choose an $op?
How to choose the $op if isn't exist bypass parameter.
i try:
function mymodule_node_insert($node) {..
}
Check API docs
Everything you need should be at http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_insert/7 (including an example).
You don't need $op because it's implied by hook_insert. Your function doesn't work because the hook name is wrong (hook_insert not hook_node_insert).
Ken Winters