I love the community support here.

I am a newbie and never tried a hook in custom modules. I was able to create a custom module and i understood a little about the working of hooks but never implemented any.

I have a content type which has a field called 'dealer' . its a computed field and it just stores the value of taxonomy term . What i need to do is to extract the tid or term from this field and make the taxonomy term attached to the node after its created in the term_node table. So i searched a bit and found out that i can use the hook_nodeapi but i cant understand how to extract the value from the node and then run the sql query to add the node in the term_node table with the tid.

Comments

nevets’s picture

A better approach would be to use the presave option, that will allow you to set the term id properly and add to $node->taxonomy. As how to find the value, it should be accessible in something like $node->field_FIELDNAME[0]['value'] though this counts on your module running after CCK and computed field storing the value in the node.

obair2’s picture

Thank you for replying and it helped but im trying to extract the taxonomy term from the 'content profile' of the user who created that node.

I was able to create a hook_nodeapi and this is its code

// Taking taxonomy term to which a user (user's content profile) belongs and assigning it to the node.
function MYMODULE_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
{
if ($op == 'presave' && $node->field_dealer)
{
// Getting the tid from referencing the current users content profile who is the node creator.
$tid=$content_profile->field_dealer_type[0]['value'];

// Getting the taxonomy name or term.
$taxterm= taxonomy_get_term($tid, $reset = FALSE);

// Assigning the taxonomy to the node. I think im going wrong here.
$node->taxonomy[$term->tid] = $taxterm;
}
}

I have a doubt about the taxonomy and how it stores the values. I know that its an array and that it has term id , vocab id and the term. But i cant figure out how to assign a taxonomy to a node if i know a tid.

obair2’s picture

Finally i was able to fix the problem by removing the field and by referencing the content profile to get its value !!

robbertnl’s picture

I am trying the same thing. Using the presave op iam able to change cck values, but changes to taxonomy are not saved. Can you please give me a suggestion what i am doing wrong?

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
if ($op == 'presave' && $node->type=='profile'){
$node->taxonomy[5]=array(0 => 161);
}

nevets’s picture

I think you want

$node->taxonomy[]= 161;