I hate to store PHP code in the database! I hate the eval function!
This patch allows you to compute values for computed fields from functions in code files.
The advantage of using computed field this way is that you can put code in php files and use version control systems on them, all without having to worry about cck hooks, formatters, views integration, etc... because computed_field takes cares of all that.
With this patch you can implement 2 functions per field: FIELD_NAME_compute_value and FIELD_NAME_display_format.
FIELD_NAME_compute_value has this format:
function FIELD_NAME_compute_value(&$node, $field, &$node_field) {
$node_field[0]['value'] = md5($node->title);
}
FIELD_NAME_display_format has this format:
function field_computed_over_display_format($field, $element) {
return $element['#node']->title .' = '. $element['#item']['value'];
}
I know the display format part could also be done with theme functions but I modified the original theme functions for consistency.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | function-code-computed.patch | 6.03 KB | Pedro Lozano |
| function-code-computed.patch | 2.53 KB | Pedro Lozano |
Comments
Comment #1
Pedro Lozano commentedUpdated by suggestions from Moonshine by email:
1. Override functions should follow Drupal naming conventions. They will follow this pattern:
computed_field_*fieldname*_computeandomputed_field_*fieldname*_display.2. Document somewhere what can be done. The description of the textareas in the field config will have an indication that the code can be overriden from a function. It will indicate the function name needed to override the code.
3. If the functions have been implemented the textareas in the field config will not appear. A message noting that the code is been overriden by a function (and its name) will be shown instead.
Comment #2
Pedro Lozano commentedI doub anyone is going to review this for me :-\. Rerolled and commited.