On this page
Working with multiple values
You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules
This example shows how to set and display multiple values for a computed field.
Enable the multiple values option for the computed field.
In earlier version of Drupal (5? 7?)
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
In Drupal 8
In Drupal 8, to populate a multi-valued field, a single value must still be returned, but your function will be called multiple times for each of your values (or "deltas"). So it's necessary to populate the full delta-keyed array, but then only return the value requested (for each delta).
Computed Code
Here is an example.
function computed_field_FIELD_NAME_compute($entity_type_manager, $entity, $fields, $delta) {
// Fill in array values with data. The keys are optional in this case as they are the defaults.
$values = [
0 => 'Dog',
1 => 'Cat',
2 => 'Mouse',
];
// Return the single value currently requested by the system.
return $values[$delta];
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion