Advertising sustains the DA. Ads are hidden for members. Join today

Computed Field

Working with multiple values

Last updated on
30 April 2025

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

Page status: Not set

You can: