I have some CCK fields defined for my content. One of the fields is a CT (content taxonomy) type field that can have multiple values, so it's something like this:

field_myctfield = Array ( 
    [0] => Array
        (
            [3] => stdClass Object
                (
                    [tid] => 3
                    [vid] => 2
                    [name] => Taxon3
                    [description] => 
                    [weight] => -8
                )

            [12] => stdClass Object
                (
                    [tid] => 12
                    [vid] => 2
                    [name] => Taxon12
                    [description] => 
                    [weight] => -5
                )

            [7] => stdClass Object
                (
                    [tid] => 7
                    [vid] => 2
                    [name] => Taxon7
                    [description] => 
                    [weight] => 0
                )
        )
)

OK, so now I am making a computed field, which needs to use the weight of the first term in the array.

Here's my problem. When I tried to enter the code to get this, using the "reset" PHP function, I am getting an error that the value is "not an array" ... when it obviously is. Here is my code:

$sortnum = 0;
$items = $node->field_myctfield[0];
$item = reset ($items);
if ($item->weight) {
    $sortnum = $age->weight + 8;
}
$node_field[0]['value'] = $sortnum;

This code works fine when I run it in the devel module code snippet box, but it does not work in computed field. What am I doing wrong? And more to the point, how do I get at that "first" value?

Comments

leeksoup’s picture

I still don't know the answer to this, but I found a workaround so I thought I'd post in case it can help someone. I just save the node first and then use the taxonomy API function taxonomy_node_get_terms_by_vocabulary to get the terms I need.

Justin Freeman’s picture

Assigned: Unassigned » Justin Freeman
Status: Active » Closed (fixed)

The solution here is to save the node first. This is required for the taxonomy functions to work. See http://drupal.org/node/149232