I was trying to find out what forum a particular node belongs to using sth. like this:

$nid = x; // the node to look for
$term = db_fetch_object(db_query('SELECT tid FROM {forum} WHERE nid = %d', $nid));
echo 'node:'.$nid.' belongs to forum:'.$term->tid;

The code works perfectly for me. But a user reported that on his install
the column 'tid' in table 'forum' is always 0 and therefore above code
doesnt work for him.

Any ideas on that?

Comments

Jonas Kvarnstrom’s picture

It happens for me too, and I'm going crazy trying to determine why given that I'm new to Drupal and don't know when and how things are intended to happen in the hook system.

Whatever I post in my test forum Drupal always claims that the post has been moved. If I fix the tid manually it does show up in the correct table.

I guess $node->tid isn't set correctly when forum_insert is called? But so far I have no idea who is supposed to set $node->tid, or what function is called when I submit a forum post.

I did change vocabularies around a bit when I was experimenting, so probably this has made Drupal confused in some way. But everything looks OK on the admin pages now, as far as I can see.

(Drupal 4.7 beta 5.)

Jonas Kvarnstrom’s picture

I finally found the answer (well, for my own version of the problem at least)!

I wanted to use the same vocabulary for forums as I do for news topics, but for news topics I wanted to be able to select multiple categories (vocabulary terms). So I had set the vocabulary to allow multiple terms for the same node.

The forum module doesn't seem to allow crossposting; it is built on the assumption that only one category can be chosen. So forum_submit() doesn't expect a multiple choice field for categories. In "foreach ($node->taxonomy as $term)", $term becomes an array (verified using debug output) which is of course not part of the term_data table and so $node->tid is never set. Then, forum_insert silently inserts tid=0 into the forums table, and the new topic never shows up.

As soon as I changed back to only allowing a single term, my debug output printed $term=14 instead of $term=Array and the topic was inserted correctly.

profix898’s picture

Never tried that before. But yes I can reproduce it following your
description. Maybe we can use code like this

foreach ($node->taxonomy as $term) {
 if (is_array($term)) {
  $term = current($term);
 }
 if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vocabulary))) {
  $node->tid = $term;
 }
}

to solve the problem. It ensures that $node->tid is always set.
The problem is howto find out what $term to use. The code above
always takes the first one ...
Can I assume that only one of the terms is in forums vocabulary?
And if not use the first one? If so the following should work as
expected.

$vocabulary = variable_get('forum_nav_vocabulary', '');
foreach ($node->taxonomy as $term) {
  //$term is an array
  if (is_array($term)) {
    $forum_terms = array();
    $results = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vocabulary);
    //iterate through all tids in forum vocab
    while ($tobj = db_fetch_object($results)) {
      $forum_terms[] = $tobj->tid; 
    }
    //go through all terms and pick the one that is in forum vocab
    foreach ($term as $tid) {
      if (in_array($tid, $forum_terms)) {
        $node->tid = $tid;
        break; //use first matching term in forum vocab (comment to use last)
      }
    }
  //$term is NOT an array
  } else if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vocabulary))) {
    $node->tid = $term;
  }
}
//it is still an array (sth. went wrong), so simply use the first term
//I THINK THIS PART IS REDUNDANT
if (is_array($term)) {
  $term = current($term);
}

Maybe you can tell me whether its working or not. (It does for me :))
So that I can post the patch to forum.module.

P.S. The code is found in function 'forum_submit' in forum.module

Jonas Kvarnstrom’s picture

Actually, I didn't have terms from multiple vocabularies; I only had terms from the forum vocabulary, but I set that vocabulary to allow multiple choices. So I got a single list box rather than a single dropdown when I wrote a message, and then even if I only selected a single item it was still wrapped in an array. If I did select multiple terms then all of them were from the forum vocabulary.

I'll test the first fix when I get the time; it should allow me to use multiple items from that vocabulary when I make ordinary news posts, while forum posts end up in the first of the selected forums, which is not exactly what I intended but it's close.

profix898’s picture

If I post a topic to 2 different terms in vocabulary forums, for example ForumA and News, the topic is actually added only two one of them and the other shows a 'This topic has been moved' message that links to the first one. But I was unable to find the reason for that by now.

EDIT:
It seems like only one term is added to table 'forum' with my code above, but you can find both terms associated to the node in table 'term_node'. I dont think, that is what we exptected, right!?

I think this is a bigger issue and makes major changes to forum.module necessary. I also think it is most critical that the node is never listed in the forums (without the code mod) when a user selects multiple terms.

mlncn’s picture

I had the same unreachable forum post in as many categories as I wished to put it in: "This topic has been moved" and the broken link to node 0. Turning off the "multiple select" option in categories and reassigning the post to a category and saving gave it a working link.

This is the fault of the Forum module and not the contributed Category module, correct?

Thanks greatly for the explanation posted here... this problem came at a bad time for me-- my first post simply doesn't show up in any category it gets assigned to (it is counted in the post count but isn't listed there). I still have this problem and it could be related to having enabled the Category taxonomy wrapper a tad late in the game (and having to recreate all my categories), but the thing was enabling the multiple category select was one of my random attempts to allow my phantom post to get listed by a category. It didn't work but when I gave up and created a new post to repost its content, I got the bogus "This topic has been moved" message and it was quite enough to drive me into a life of desolute wanderings on the thawing Siberian plains.

Also in regular operation the "Leave a shadow option" does nothing at all if I move a topic. Looking forward to a new & improved category module (which will import all my existing categories and posts)...

~ben http://bemweb.com/contact/

benjamin melançon, human being

web site programmer/designer & digital photographer, http://bemweb.com/
now member, Agaric Design Collective
http://AgaricDesign.com - "Open Source Web Development"

web worker, PowerToExchange
http://PowerToExchange.com - "Of, By, and For Entrepreneurs"

person, People Who Give a Damn
http://pwgd.org - "building the infrastructure for a network of everyone"

benjamin, Agaric

danroth’s picture

Please see related discussion at:
http://drupal.org/node/40394

summit’s picture

Bookmarking, greetings, Martijn