Add a term to a node (5.x)
For Drupal 5.x
<?php
/*
* Implementation of a Drupal action.
* Assigns a node to a taxonomy term.
*/
function action_node_assign_term($op, $edit = array(), &$node) {
switch($op) {
case 'metadata':
return array(
'description' => t('Assign taxonomy term to node'),
'type' => t('Taxonomy'),
'batchable' => true,
'configurable' => true,
);
case 'do':
$tid = intval($edit['term_id']);
$existing = taxonomy_node_get_terms($node->nid);
//does it exist already?
if(!array_search($tid, array_keys($existing))) {
$existing[$tid] = $tid;
taxonomy_node_save($node->nid, $existing);
}
break;
case 'form':
// add form components
$desc = t('The node will be assigned to the term with this id.');
$form['term_id'] = array(
'#type' => 'select',
'#title' => t("Term"),
'#default_value' => $edit['term_id'],
'#options' => taxonomy_form_all(),
'#multiple' => FALSE,
'#size' => 10,
'#theme' => 'taxonomy_term_select'
);
return $form;
case 'validate':
if (!is_numeric($edit['term_id'])) {
form_set_error('term_id', t('This is no valid termid'));
return false;
}
return true;
// process the HTML form to store configuration
case 'submit':
$params = array('term_id' => $edit['term_id']);
return $params;
}
}
/*
* Implementation of a Drupal action.
* Removes the assignment of a node to a taxonomy term
*/
function action_node_remove_term($op, $edit = array(), &$node) {
switch($op) {
case 'metadata':
return array(
'description' => t('Remove a taxonomy term of a node'),
'type' => t('Taxonomy'),
'batchable' => true,
'configurable' => true,
);
case 'do':
$tid = intval($edit['term_id']);
$existing = taxonomy_node_get_terms($node->nid);
if(array_search($tid, array_keys($existing))) {
unset($existing[$tid]);
}
taxonomy_node_save($node->nid, $existing);
break;
case 'form':
// add form components
$desc = t('The term with this id will be removed from this node.');
$form['term_id'] = array(
'#type' => 'select',
'#title' => t("Term"),
'#default_value' => $edit['term_id'],
'#options' => taxonomy_form_all(),
'#multiple' => FALSE,
'#size' => 10,
'#theme' => 'taxonomy_term_select'
);
return $form;
case 'validate':
if (!is_numeric($edit['term_id'])) {
form_set_error('term_id', t('This is no valid termid'));
return false;
}
return true;
// process the HTML form to store configuration
case 'submit':
$params = array('term_id' => $edit['term_id']);
return $params;
}
}
?>
Fix this action so that it also updates node_access?
I was trying out this code with a combination of the actions, votingapi, and voting_actions modules to have a term added/removed from a node based on the vote result.
While this does successfully add/remove tax terms, it does not cause the node access table to be updated, which is problematic since I also use Taxonomy Access Control module to restrict access to certain terms. What else must added (i.e. a hook in taxonomy_access module) so that this code also causes the node_access table to be updated appropriately?
Use node_save instead of taxonomy_node_save()
Apparently, using taxonomy_node_save() only results in an update to the node_term table, no others (e.g. the node_access table).
A suggested patch for the action code above to play nicely with node access modules like TAC...
Replace both instance of this:
taxonomy_node_save($node->nid, $existing);with this:
$node->taxonomy = array_keys($existing);node_save($node);