Add a term to a node (4.7)
This action will add a taxonomy term to a node by doing an INSERT on the term_node table. You need to know the term's ID number to put in the configuration screen. You can also simply edit the db_query to do a DELETE or UPDATE statement as well for deleting or changing the term on a node.
<?php
function action_add_taxonomy($op, $edit = array(), &$node) {
switch ($op) {
case 'metadata' :
return array(
'description' => t('Add a taxonomy term to a node'),
'type' => t('Node'),
'batchable' => false,
'configurable' => true);
case 'do' :
if(!isset($edit['term'])){
// log
watchdog('error', t('Action Term: no term has been identified!'));
break;
}
// get the term we added in config
$termID = $edit['term'];
// log
watchdog('action', t('Added term ID '. $termID .' to node "'. $node->title .'".'));
// insert the new nid-tid association in the db
db_query('INSERT into {term_node} (nid, tid) VALUES (%d, %d)', $node->nid, $termID);
break;
// return an HTML config form for the action
case 'form' :
// default values for form
$form = array ();
$form['term'] = array (
'#type' => 'textfield',
'#title' => 'Term to add',
'#default_value' => $edit['term'],
'#maxlength' => 5,
'#collapsible' => FALSE,
'#required' => TRUE,
'#description' => t('Enter the taxonomy term ID number that you would like added to the node. Just the number, NO slashes or text!'),
);
return $form;
// validate the HTML form
case 'validate' :
if (!is_numeric($edit['term'])) {
form_set_error('term', t('This is not a valid term ID. Must be a number, not a name.'));
return false;
}
return true;
// process the HTML form to store configuration
case 'submit' :
$params = array (
'term' => $edit['term'],
);
return $params;
}
}
?>