Node comment inherits it's parent nodes taxonomy
tuti - September 17, 2007 - 05:36
| Project: | Node comments |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
When a user comments on a node with several taxonomy terms associated with it I'd like the node comment to also get the same taxonomy associations. Any tips/comments on how this would be possible is appreciated!
Thanks!

#1
Use hook_formalter in a custom module to load the parent node and apply the terms to the child node. If you want to enforce this cascading without the user having the chance to change things you can use hook_nodeapi insert/update to do the same without requiring form interaction.
#2
Thanks for the fast reply. I'm a total php newbie. I guess I should use some info from here?
Is it a custom module when using hook_nodeapi also? Or where does the php code go?
I found the edit as new module and found some code that may be useful? (Node Clone has replaced edit as new)
Can this code be used to make the node comment copy the taxonomy terms of it's parent?
<?php
function editasnew_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
// if a new node is being created and it was created from the "edit as new" tab,
// it should have a template defined and used to create this one.
case 'validate':
$template_nid = arg(4); // e.g. node/add/template/123
if(arg(3) == 'template' && is_numeric($template_nid)) {
$template_node = node_load(array('nid'=>$template_nid));
if (module_exist("taxonomy")) { //+
$template_node->taxonomy = array_keys(taxonomy_node_get_terms($template_nid)); //+
} //+
?>
#3
So you've got the bug for Drupal programming... good =) Go get the VanDyk/Westgate book to get a thorough lesson in doing Drupal programming. You'll enjoy it.
Any module can have a nodeapi hook and it will get called when things happen with nodes - any nodes. So if you make your own module and it has a nodeapi hook, you'll be able to alter the nodes when they get created. The code you posted is a good study point and you can learn some of what needs to be done from it. There is no module that will do exactly what you're asking for, though, so there is no code that can be just copied and pasted. Here's a trick: in the nodeapi hook, use the print_r function to see what's in the $node object. This will give you some clues. Another trick is that when you find the parent node's id, you'll need to load it like this:
$parent_node = node_load($parent_id));
Then the parent node will have a taxonomy array: $parent_node->taxonomy
Use array_merge to put that into the child node: $node->taxonomy = array_merge($node->taxonomy, $parent_node->taxonomy);
Have fun - Drupal programming is about to change your life :D
#4
I'm poping up this thread because I exactly have the same need. Is a contrib coming out from this need ?
#5
Subscribing.
#7
Just tried this, but it doesn't work as comments don't have a taxonomy field in the $node object. I've hacked a new funtion to adjust my views as follows. The effect is to hide any comments in the recent comments block that are attached to nodes with the categories in $terms. The only drawback is that it happens after the query is executed, so yo may end up with an empty block. It does of course require the views module to be installed and in control of said block.
function hook_views_pre_view($view, &$items) {
if ($view->name =='comments_recent') {
$terms = array('Staff only', 'Staff documents');
foreach ($items as $itemkey=>$item) {
$parent_node = node_load($item->nid);
$keys = array_keys($parent_node->taxonomy);
$key = $keys[0];
if (in_array($parent_node->taxonomy[$key]->name, $terms)) {
unset($items[$itemkey]);
}
}
}
}
#8
Whoops. Found this thread via search and totally didn't notice the module it was attached to. Please ignore!
#9
subscribing. I need this too