I get the following errors:

# notice: Object of class stdClass could not be converted to int in modules\forum\forum.module on line 248.
# notice: Object of class stdClass could not be converted to int in modules\forum\forum.module on line 253.
# notice: Object of class stdClass could not be converted to int in includes\database.inc on line 210.

This is caused by taking the $node->taxonomy elements as term_ids when they really are term objects.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joshmiller’s picture

Title: Errors when batch publishing a forum topic » Forum doesn't handle taxonomy term objects properly.
Version: 6.x-dev » 7.x-dev
FileSize
701 bytes

The title wasn't informative of what is being accomplished here.

The use case: Errors when batch publishing a forum topic

The problem: Forum doesn't handle taxonomy term objects properly.

I've looked at the three lines of code and they accomplish a simple type change from object to int very nicely. Re-rolled patch against D7.

catch’s picture

Status: Needs review » Needs work

Is this code not tested in core? Does it sometimes get an int and sometimes an object? Marking to CNW since either one of those questions needs an answer before we commit this I think.

eojthebrave’s picture

Just FYI, I've been unable to reproduce this problem.

Steps taken to try and reproduce the problem.

1. Enable forum module
2. Create a new forum for testing, admin/build/forum
3. Create a couple of new forum topics, mark them as un-published.
4. Navigate to admin/content/node, select the un-published forum topics and then publish them.

This was done on a clean install of Drupal HEAD.

Looking at the code I'm not sure that this is relevant for Drupal 7.

I was also unable to re-produce the error in Drupal 6.10

Can anyone confirm that this is still a problem? Can anyone provide detailed instructions as to how someone could reproduce the problem.

catch’s picture

Status: Needs work » Postponed (maintainer needs more info)
salvis’s picture

Title: Forum doesn't handle taxonomy term objects properly. » Notices when batch publishing a forum topic
Status: Postponed (maintainer needs more info) » Needs review

The terms in $node->taxonomy are usually objects. However, when implementing hook_node_access_records() I've found out the hard way, that $node->taxonomy can contain elements of one of three types (in D6):

  • tid => term
  • vid => array(tid => tid)
  • vid => tid

The first case is the normal one. The current code only works in the third case (the variable name $term_id is a give-away), which means it's not working:

    // Assign forum taxonomy when adding a topic from within a forum.
    case 'presave':
      // Make sure all fields are set properly:
      $node->icon = !empty($node->icon) ? $node->icon : '';

      if ($node->taxonomy && $tree = taxonomy_get_tree($vid)) {
        // Get the forum terms from the (cached) tree if we have a taxonomy.
        foreach ($tree as $term) {
          $forum_terms[] = $term->tid;
        }
        foreach ($node->taxonomy as $term_id) {
          if (in_array($term_id, $forum_terms)) {        // FAILURE!
            $node->tid = $term_id;
          }
        }

The "errors" that I mentioned in the OP are actually E_NOTICEs...

notice: Object of class stdClass could not be converted to int in drupal-6\modules\forum\forum.module on line 254.

... from the in_array() call, and $node->tid is not set, because the condition is false. I don't know whether the failure to set $node->tid has any nasty consequences later on, but it's clearly a bug.

You need to have the cvs version of Drupal to see notices. Or you can set a breakpoint on line 254 and look at what you get for $term_id. Or you can insert
if (is_object($term_id)) drupal_set_message('BUG', 'error');
above the in_array() call.

Note that this issue was posted in January 2008. I don't know whether it applies to D7 and I'm not going to find out. I do know that it applies to D6.

cburschka’s picture

To find out how $node->taxonomy is handled in Drupal 7, the best reference is taxonomy's own saving function.

      if (is_array($term)) {
        foreach ($term as $tid) {
          if ($tid) {
            $query->values(array(
              'nid' => $node->nid,
              'vid' => $node->vid,
              'tid' => $tid,
            ));
          }
        }
      }
      elseif (is_object($term)) {
        $query->values(array(
          'nid' => $node->nid,
          'vid' => $node->vid,
          'tid' => $term->tid,
        ));
      }
      elseif ($term) {
        $query->values(array(
          'nid' => $node->nid,
          'vid' => $node->vid,
          'tid' => $term,
        ));
      }

As we can see, taxonomy itself codes for the possibility of each element of the $node->taxonomy array being either an object, an array of tids, or a tid. When loading node objects, taxonomy generates $node->taxonomy as an array of objects. (Note that $node->taxonomy['tags'] is a special array of [ int vid => string term-names ] for freetagging, which is processed and removed separately.)

cburschka’s picture

Status: Needs review » Needs work

It stands to reason that forum_node_presave will need to code for all of these cases. Unless we can show that some of these cases will never occur in the data that forum_node_presave is passed.

criznach’s picture

Status: Needs work » Needs review
FileSize
1.58 KB

Here's a patch for 6.13 that should handle the 3 cases above. I've only found a test case for the first scenario (array of objects keyed by tid), so I'll need some help reviewing it for the other cases. In our case, it solved issues with the scheduler module. I'll try to get a patch for 7.x-dev as well.

Status: Needs review » Needs work

The last submitted patch failed testing.

aharown07’s picture

Tested the 6.x version. Corrects the problem I was seeing... custom content types w/forum taxonomy now save properly when Scheduler publishes them (and appear in the forums).

criznach’s picture

Status: Needs work » Needs review
FileSize
1.51 KB

Ok, here's a patch for 7.x-dev. To test this using only core modules, do this....

Enable Trigger and Forum core modules.
Assign the "Save post" action to the "After saving a new comment" trigger.
Create a forum container.
Create a forum inside that container.
Create a post in the forum.
Reply to the forum post with a comment.
Now try to view your post. It will be gone from the forum because it's record in the forum table has been deleted.

Again, I can only come up with a test case for the first scenario in the code. I can't think of situations where $node->taxonomy is a single term object, or single tid, but apparently they exist.

Status: Needs review » Needs work

The last submitted patch failed testing.

criznach’s picture

Status: Needs work » Needs review
FileSize
1.79 KB

My bad... This should pass all tests.

salvis’s picture

@criznach: I found that during [Rebuild permissions] the elements of $node->taxonomy are of the tid=>term type. During node creation they're vid=>tid for single-select taxonomies and vid=>array(tid=>tid) for multi-select taxonomies (D6).

criznach’s picture

I think one key difference is that one format comes from the form api, and another comes from db_fetch_object.

Status: Needs review » Needs work

The last submitted patch failed testing.