Last updated February 2, 2012. Created by ksenzee on June 4, 2007.
Edited by colan, LGLC, peterpoe, elcuco. Log in to edit this page.
Configuration
A Computed Field can be configured with the usual field options, as well as the following extra options:
- Computed Code
- This is the code that will assign a value to your computed field. It should be valid PHP without the <?php ?> tags.
- Display Code
- This is also PHP code which should assign a string to the $display_output variable. It has
$node_field_item['value'](pre-D7) or$entity_field_item['value'](D7+) available, which is the value of the computed field. It also has$field(and some language information, D7+) available, and you can call any Drupal functions you want to display your field. Alternately, the code can be supplied by your own custom function named computed_field_YOUR_FIELD_MACHINE_NAME_display(). See the main Computed Field documentation page for an example of how to implement it. - Store using the database settings below
- If this is checked then the field is computed on node/entity save and stored. If it isn't stored then it will be recomputed every time you view a node/entity containing this field. This option is required if you wish to access your field with the Views module.
- Database Storage Settings
-
- Data Type
- This is the SQL data type to use for storing the field contents in the database. Let us know if you need any other storage types, or if you would like an 'other' option :). For calculating dollars and cents, for example, you would choose the "decimal" option.
- Data Length
- This value will simply be passed on to SQL. For storing up to 10 digit integers (INTs), enter 10. For storing currency as a float, use 10,2 (unless you'll store larger than 10 figure amounts). For storing usernames or other short text with a VARCHAR field, 64 may be appropriate.
- Default Value
- Leave this blank if you don't want the database to store a default value if your computed field's value isn't set. Following the currency example above, you would set this to "0.00".
- Not NULL
- Leave unchecked if you want to allow NULL values in the database field. For things like currency, you probably want to check this.
- Sortable
- Used in Views to allow sorting a column on this field. (Pre-D7 only. May not be required in D7+.)
- Indexed
- Adds a simple DB index on on the computed values.
Tips
- If you want your field visible in Views, then store it in the database.
- If your stored field needs to access the node ID, then add
if (!$node->nid) node_save($node);at the start of your computed code. Otherwise the node might not yet have an nid. For Drupal 7+, use the $entity object instead of the $node object.
Comments
In drupal 7, this tip wont
In drupal 7, this tip wont work:
"If your stored field needs to access the node id, then add if (!$node->nid) node_save($node); at the start of your computed code. Otherwise the node might not yet have an nid."
Shouldn't
(!$entity->nid) node_save($entity);DO the trick in Drupal 7 or im missing something here?
Currency storage
The tip above about using float and 10,2 for storing currency will not work with the standard 6.x version of the module. Currently, you need to use the dev version of the module to be able to use the "decimal" datatype for your fields. With this version you can store currency values in a computed field.
Drupal Developer
External Computed Code in Drupal 7
It looks like the new syntax for this is:
<?phpfunction computed_field_FIELD-NAME_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
// Change the value of the field
$entity_field[0]['value'] = 'foo';
}
?>
Computed Field External Display Function in Drupal 7
Looks like the corresponding new syntax for this is:
<?phpfunction computed_field_FIELDNAME_display($field, $entity_field_item, $entity_lang, $langcode) {
// Return the value to be displayed. Original value is in $entity_field_item['value'].
return $entity_field_item['value'];
}
?>
display function?
How about the display function? I'm having a heck of a time getting that to register.
(Drupal 6)