Configuring Computed Field

Last modified: July 20, 2009 - 06:24

Configuration

A Computed Field can be configured with the usual cck 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 this field
Check this box to have this field appear on your node view pages. You will usually want this unless you want your field to be a hidden value.
Display Format
This is also php code which should assign a string to the $display variable. It has $node_field_item[&#039;value&#039;] available, which is the value of the computed field. It also has $field available, and you can call any drupal functions you want to display your field.
Store using the database settings below
If this is checked then the field is computed on node save and stored. If it isn't stored then it will be recomputed every time you view a node containing this field.
Database Storage Settings
Data Type
This is the sql data type to use to store the field. Let us know if you need any other storage types, or if you would like an 'other' option :).
Data Length
This value will simply be passed on to sql. For storing up to 10 digit 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.
Not NULL
Leave unchecked if you want to allow NULL values in the database field.
Sortable
Used in Views to allow sorting a column of this field.

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.
  • If you have a computed field that depends on another, then give it a higher weight. Otherwise you will get an older value of the field, or none at all.
 
 

Drupal is a registered trademark of Dries Buytaert.