Here is the code for my computed field:

$node_field[0]['value'] = $node->taxonomy[tags][7] ." ". $node->title;

So in the end my field should look like this: [taxonomy term] [title]

This field updated correctly if I edit the node and then re-save it. Upon using your module on 57,000+ nodes, each one shows up as just [title]. So it actually did compute the field (otherwise it would be blank), it just computed it incorrectly and didn't pull the taxonomy for some reason. Trying again resulted in 57,000 "duplicate entry" errors.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

frakke’s picture

The taxonomy handling is a bit tricky. When editing nodes, the taxonomy is in one format, and when re-computing it is in a different format. The code below should do the trick. It might not fit your needs excactly, bu it should give you the basic idea :)

  // Set given terms by node language
  // We have different vocabularies with the same role for each langauge...
  $vids_by_name_and_language = array(
    'term_section' => array(
      'da-DK' => 1,
      'sv-SE' => 10,
    ),
    'term_category' => array(
      'da-DK' => 2,
      'sv-SE' => 3,
    ),
  );

  // Teaser terms. Set the terms by language.
  // This has to take into account both different vids by language and the built in
  // messed up handling of terms when saving nodes and recomputing nodes, so thats why
  // it might seem a bit messy...
  if (!empty($node->taxonomy) && !empty($node->language)) {
    $taxonnomies_to_save = array_keys($vids_by_name_and_language);
    // Just grap the first comming term of given vid...
    foreach ($node->taxonomy as $key => $term) {
      // The node is just beeing re-computed with the re-compute field
      if (is_object($term)) {
        foreach ($taxonnomies_to_save as $name) {
          // Check to see if we have a vid to check on.
          if (empty($vids_by_name_and_language[$name][$node->language])) {
            continue;
          }
          $vid = $vids_by_name_and_language[$name][$node->language];
          if ($term->vid == $vid) {
            $teaser_values[$name . '_tid'] = $term->tid;
          }
        }
      }
      // The node is being created or updated, and the $key is not the vid...
      elseif (is_array($term) && !empty($term)) {
        foreach ($taxonnomies_to_save as $name) {
          // Check to see if we have a vid to check on.
          if (empty($vids_by_name_and_language[$name][$node->language])) {
            continue;
          }
          $vid = $vids_by_name_and_language[$name][$node->language];
          if ($key == $vid) {
            // Just grap the first term.
            $tid = current($term);
            if ($term_loaded = taxonomy_get_term($tid)) {
              $teaser_values[$name . '_tid'] = $term_loaded->tid;
            }
          }
        }
      }
      elseif (is_string($term) && !empty($term)) {
        // The node is still being created or updated, but we only have one term.
        foreach ($taxonnomies_to_save as $name) {
          // Check to see if we have a vid to check on.
          if (empty($vids_by_name_and_language[$name][$node->language])) {
            continue;
          }
          $vid = $vids_by_name_and_language[$name][$node->language];
          if ($key == $vid) {
            // Just grap the first term.
            if ($term_loaded = taxonomy_get_term($term)) {
              $teaser_values[$name . '_tid'] = $term_loaded->tid;
            }
          }
        }
      }
    }
  }
frakke’s picture

Assigned: Unassigned » frakke
Status: Active » Closed (won't fix)

From the README.txt:
"Please note that when you re-compute the nodes, the node is fetched through node_load() which means that the format of some values might defer from when you submit the node through the node edit form. $node->taxonomy does this."

This is not a bug. This is just the way is has to be. The alternative would be, that I had to emulate a node submit which would in my opinion be a rather more messy solution with many potential pitfalls.

dervishmoose’s picture

Another work around is to not use the use $node->taxonomy but to get the terms via taxonomy_node_get_terms in the computed field. This will work in both the edit form and in re-compute and in a views bulk operation.

edit: oops, I see this code will only work in D6. However, I think something similar can be done in D7.

$vterms = taxonomy_node_get_terms($node);
$vitems=array();
foreach ( $vterms as $vterm ) {
        $vitems[]=$vterm->name;
    }

$node_field[0]['value'] =implode(", ", $vitems);

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.