Working with multiple values
Last modified: April 22, 2008 - 20:15
This example shows how to set and display multiple values for a computed field.
Enable the multiple values option for the computed field.
Computed Code
$node_field[0]['value'] = "en.wikipedia.org/wiki/Scrapheap_Challenge";
$node_field[1]['value'] = "abc.net.au";
$node_field[2]['value'] = "australia.gov";Enable the 'display this field' option for the computed field.
Display Format
$display = l('A link to '.$node_field_item['value'],'http://'.$node_field_item['value']);When computed field executes the output will be displayed as:
A link to en.wikipedia.org/wiki/Scrapheap_Challenge
A link to abc.net.au
A link to australia.gov

Use A for() Loop On CCK Field Array
Using Drupal 6 for my examples. This is when in CCK you set Number of values: to more then 1.
Computed Code
//Iterate through array. Output x, x, x, x and x
//set temp variables
$temp_field_x_array_length = count($node->field_x);
$temp_field_x_output = "";
$temp_field_x_counter = 0;
//main loop
for ($temp_field_x_counter = 0; $temp_field_x_counter < $temp_field_x_array_length-2; $temp_field_x_counter+= 1)
{
$temp_field_x_output .= $node->field_x[$temp_field_x_counter]['value'] . ", ";
}
//second to last value
if ($temp_field_x_counter == $temp_field_x_array_length-2)
{
$temp_field_x_output .= $node->field_x[$temp_field_x_counter]['value'] . " and ";
$temp_field_x_counter +=1;
}
//last value
if ($temp_field_x_counter == $temp_field_x_array_length-1)
{
$temp_field_x_output .= $node->field_x[$temp_field_x_counter]['value'] . "";
$temp_field_x_counter +=1;
}
$node_field[0]['value'] = $temp_field_x_output;
Display Format
$display = $node_field_item['value'];If your using the Content Taxonomy field change this
$temp_field_x_output .= $node->field_x[$temp_field_x_counter]['value'] .into this for plain text output
$temp_field_x_term = taxonomy_get_term($node->field_x[$temp_field_x_counter]['value']);$temp_field_x_output .= check_plain($temp_field_x_term->name) .
or this for linked output
$temp_field_x_term = taxonomy_get_term($node->field_x[$temp_field_x_counter]['value']);$temp_field_x_output .= l($temp_field_x_term->name, taxonomy_term_path($temp_field_x_term), array('rel' => 'tag', 'title' => $temp_field_x_term->description));
Grabbed from line 200 of content_taxonomy.module
Add in $temp_field_x_term = ""; at the top in order to declare your variable before using it. Also be aware that the ""; is missing from above so that you can use find and replace for the code.
Display First and Last Value Only
Computed Code
//display first and last value only: x - x
//set temp variables
$temp_field_x_array_length = count($node->field_x);
$temp_field_x_output = "";
//zero to one value
if ($temp_field_x_array_length == 1)
{
$temp_field_x_output = $node->field_x[0]['value'];
}
//over one value
else
{
$temp_field_x_output .= $node->field_x[0]['value'];
$temp_field_x_output .= " - ";
$temp_field_x_output .= $node->field_x[$temp_field_x_array_length-1]['value'];
}
$node_field[0]['value'] = $temp_field_x_output;
Display Code
$display = $node_field_item['value'];Display values as select box
And is it also possible the display the values as a select box? I don't think thats possible using the "display format"?
It does state however I can supply my own computed_field_field_FIELDNAME_compute() function. But where do I put that function? And are there any examples for this kind of function?