Programatically adding taxonomy term to node

Events happening in the community are now at Drupal community events on www.drupal.org.
gallamine's picture

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

afreeman's picture

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 =

kostajh's picture

Try:

<?php
$node
->taxonomy = $newterm;
node_save($node);
?>

Not sure what exactly you are

sheena_d's picture

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

gallamine's picture

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

chetanvsquare's picture

I already had 16 thousand nodes,for all that node i want to assign terms id's

I'm an idiot. Typo. The

gallamine's picture

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

davidd07's picture

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

utilisateur_drupal's picture

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

fchandler's picture

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?

rta's picture

the function mymodule_nodeapi nay work on D7?

hook_nodeapi was split in D7

Separate hooks (mostly)based on old operations

cgmonroe's picture

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?

rta's picture

How to choose the $op if isn't exist bypass parameter.
i try:

function mymodule_node_insert($node) {
..
}

Check API docs

kwinters's picture

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

triDUG

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: