| 1 |
<?php |
<?php |
| 2 |
// $Id: computed_field.module,v 1.17.2.6 2009/05/19 00:44:17 moonshine Exp $ |
// $Id: computed_field.module,v 1.17.2.7 2009/07/31 23:26:18 pl2 Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of cck hook_field_info() |
* Implementation of cck hook_field_info() |
| 24 |
switch ($op) { |
switch ($op) { |
| 25 |
case 'form': |
case 'form': |
| 26 |
$form = array(); |
$form = array(); |
| 27 |
|
$compute_func = 'computed_field_'. $field['field_name'] .'_compute'; |
| 28 |
|
$display_func = 'computed_field_'. $field['field_name'] .'_display'; |
| 29 |
// these next 3 have been moved from widget to field, so they copy default values from widget |
// these next 3 have been moved from widget to field, so they copy default values from widget |
| 30 |
$form['code'] = array( |
$form['code'] = array( |
| 31 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 32 |
'#rows' => 15, |
'#rows' => 15, |
| 33 |
'#title' => t('Computed Code'), |
'#title' => t('Computed Code'), |
| 34 |
'#description' => t('The variables available to your code are: ') .'<code>&$node, $field, and &$node_field</code>'. t('. To set the value of the field, set ') .'<code>$node_field[0][\'value\']</code>'. t('. Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields field_a and field_b: ') .'<code>$node_field[0][\'value\'] = $node->field_a[0][\'value\'] + $node->field_b[0][\'value\'];</code>', |
'#description' => t('The variables available to your code are: ') .'<code>&$node, $field, and &$node_field</code>'. t('. To set the value of the field, set ') .'<code>$node_field[0][\'value\']</code>'. t('. Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields field_a and field_b: ') .'<code>$node_field[0][\'value\'] = $node->field_a[0][\'value\'] + $node->field_b[0][\'value\'];</code>. '. t('Alternately, this code can be supplied by your own custom function named @compute_func().', array('@compute_func' => $compute_func)), |
| 35 |
'#default_value' => !empty($field['code']) ? $field['code'] : '$node_field[0][\'value\'] = "";', |
'#default_value' => !empty($field['code']) ? $field['code'] : '$node_field[0][\'value\'] = "";', |
| 36 |
|
'#access' => !function_exists($compute_func), |
| 37 |
); |
); |
| 38 |
|
if (function_exists($compute_func)) { |
| 39 |
|
$form['compute_func'] = array( |
| 40 |
|
'#type' => 'item', |
| 41 |
|
'#value' => t('This field is computed using @compute_func().', array('@compute_func' => $compute_func)), |
| 42 |
|
); |
| 43 |
|
} |
| 44 |
$form['display_format'] = array( |
$form['display_format'] = array( |
| 45 |
'#type' => 'textarea', |
'#type' => 'textarea', |
| 46 |
'#title' => t('Display Format'), |
'#title' => t('Display Format'), |
| 47 |
'#description' => t('This code should assign a string to the $display variable, which will be printed as the value of the field. The stored value of the field is in $node_field_item[\'value\']. Note: this code has no effect if you use the "Computed Value" formatter option.'), |
'#description' => t('This code should assign a string to the $display variable, which will be printed as the value of the field. The stored value of the field is in $node_field_item[\'value\']. Note: this code has no effect if you use the "Computed Value" formatter option. Alternately, this code can be supplied by your own custom function named @display_func().', array('@display_func' => $display_func)) , |
| 48 |
'#default_value' => !empty($field['display_format']) ? $field['display_format'] : '$display = $node_field_item[\'value\'];', |
'#default_value' => !empty($field['display_format']) ? $field['display_format'] : '$display = $node_field_item[\'value\'];', |
| 49 |
|
'#access' => !function_exists($display_func), |
| 50 |
); |
); |
| 51 |
|
if (function_exists($display_func)) { |
| 52 |
|
$form['display_func'] = array( |
| 53 |
|
'#type' => 'item', |
| 54 |
|
'#value' => t('This field is computed using @display_func().', array('@display_func' => $display_func)), |
| 55 |
|
); |
| 56 |
|
} |
| 57 |
$form['store'] = array( |
$form['store'] = array( |
| 58 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 59 |
'#title' => t('Store using the database settings below'), |
'#title' => t('Store using the database settings below'), |
| 151 |
} |
} |
| 152 |
|
|
| 153 |
function _computed_field_compute_value(&$node, $field, &$node_field) { |
function _computed_field_compute_value(&$node, $field, &$node_field) { |
| 154 |
if (isset($field['code'])) { |
// Allow the value to be computed from code not stored in DB |
| 155 |
eval($field['code']); |
$compute_func = 'computed_field_'. $field['field_name'] .'_compute'; |
| 156 |
|
if (function_exists($compute_func)) { |
| 157 |
|
$compute_func($node, $field, $node_field); |
| 158 |
|
} |
| 159 |
|
else { |
| 160 |
|
if (isset($field['code'])) { |
| 161 |
|
eval($field['code']); |
| 162 |
|
} |
| 163 |
} |
} |
| 164 |
} |
} |
| 165 |
|
|
| 287 |
$field = content_fields($element['#field_name']); |
$field = content_fields($element['#field_name']); |
| 288 |
// For "some" backwards compatibility |
// For "some" backwards compatibility |
| 289 |
$node_field_item['value'] = $element['#item']['value']; |
$node_field_item['value'] = $element['#item']['value']; |
| 290 |
eval($field['display_format']); |
|
| 291 |
|
// Allow the value to be formated from code not stored in DB |
| 292 |
|
$display_func = 'computed_field_'. $field['field_name'] .'_display'; |
| 293 |
|
if (function_exists($display_func)) { |
| 294 |
|
$display = $display_func($field, $element); |
| 295 |
|
} |
| 296 |
|
else { |
| 297 |
|
eval($field['display_format']); |
| 298 |
|
} |
| 299 |
return $display; |
return $display; |
| 300 |
} |
} |
| 301 |
|
|
| 306 |
$field = content_fields($element['#field_name']); |
$field = content_fields($element['#field_name']); |
| 307 |
// For "some" backwards compatibility |
// For "some" backwards compatibility |
| 308 |
$node_field_item['value'] = $element['#item']['value']; |
$node_field_item['value'] = $element['#item']['value']; |
| 309 |
eval($field['display_format']); |
|
| 310 |
|
// Allow the value to be formated from code not stored in DB |
| 311 |
|
$display_func = 'computed_field_'. $field['field_name'] .'_display'; |
| 312 |
|
if (function_exists($display_func)) { |
| 313 |
|
$display = $display_func($field, $element); |
| 314 |
|
} |
| 315 |
|
else { |
| 316 |
|
eval($field['display_format']); |
| 317 |
|
} |
| 318 |
return check_plain($display); |
return check_plain($display); |
| 319 |
} |
} |
| 320 |
|
|
| 325 |
$field = content_fields($element['#field_name']); |
$field = content_fields($element['#field_name']); |
| 326 |
// For "some" backwards compatibility |
// For "some" backwards compatibility |
| 327 |
$node_field_item['value'] = $element['#item']['value']; |
$node_field_item['value'] = $element['#item']['value']; |
| 328 |
eval($field['display_format']); |
|
| 329 |
|
// Allow the value to be formated from code not stored in DB |
| 330 |
|
$display_func = 'computed_field_'. $field['field_name'] .'_display'; |
| 331 |
|
if (function_exists($display_func)) { |
| 332 |
|
$display = $display_func($field, $element); |
| 333 |
|
} |
| 334 |
|
else { |
| 335 |
|
eval($field['display_format']); |
| 336 |
|
} |
| 337 |
return check_markup($display); |
return check_markup($display); |
| 338 |
} |
} |
| 339 |
|
|