At the moment there is no way of removing a single (or group of) taxonomy terms for a given node. Say I have a node which has three vocabularies associated with it there are no functions which can remove one term from the node and leave the others unchanged.
I propose to modify the function taxonomy_node_delete($node) to add an optional argument $tid which is a tid to remove (it could possibly be a mixed id and then you could remove a group of tids simultaneously) and the function removes only theses associations. This is a simple modification from the current function which looks like this:
function taxonomy_node_delete($node) {
db_query('DELETE FROM {term_node} WHERE nid = %d', $node->nid);
}
Into something like this:
function taxonomy_node_delete($node, $tid = NULL) {
$sql = 'DELETE FROM {term_node} WHERE nid = %d';
if ($tid !== NULL) {
/* TODO: check if tid is an array and use 'tid in (tid1, tid2, ... )' if its an array. */
db_query('DELETE FROM {term_node} WHERE nid = %d AND tid = %d', $node->nid, $tid);
} else {
db_query('DELETE FROM {term_node} WHERE nid = %d', $node->nid);
}
}
I came across this in a module I have where I have an optional taxonomy to mark a particular content. I don't have a way to 'unmark' this node. The only way to do this is to get the terms associated with the node, remove all of them and then add only the ones I want to keep. This modification would leave everything working as it is but would allow module writers to remove a single o group of terms from a node.
I don't have a problem with writing a patch for this. I just want to make sure someone who know the taxonomy.module better and me tell me if this patch is likely to be accepted.
Comments
Comment #1
cesarmiquel commentedI noticed the above code is incorrect. An unused $sql variable was left. I also added a check to make sure its a non-zero integer. Here it goes again:
Comment #2
giorgio79 commentedAs I understand we need to post such requests for 7x dev for core modules. It may be late, but this was just what I was looking for. Thanks :)
Comment #3
catchThis is now obsolete due to field API conversion.