| 1 |
<?php
|
| 2 |
// $Id: computed_field.module,v 1.17.2.6 2009/05/19 00:44:17 moonshine Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of cck hook_field_info()
|
| 6 |
*/
|
| 7 |
function computed_field_field_info() {
|
| 8 |
return array(
|
| 9 |
'computed' => array(
|
| 10 |
'label' => t('Computed'),
|
| 11 |
'description' => t('Create field data via PHP code.'),
|
| 12 |
'callbacks' => array(
|
| 13 |
'tables' => CONTENT_CALLBACK_DEFAULT,
|
| 14 |
'arguments' => CONTENT_CALLBACK_DEFAULT,
|
| 15 |
),
|
| 16 |
),
|
| 17 |
);
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of cck hook_field_settings()
|
| 22 |
*/
|
| 23 |
function computed_field_field_settings($op, $field) {
|
| 24 |
switch ($op) {
|
| 25 |
case 'form':
|
| 26 |
$form = array();
|
| 27 |
// these next 3 have been moved from widget to field, so they copy default values from widget
|
| 28 |
$form['code'] = array(
|
| 29 |
'#type' => 'textarea',
|
| 30 |
'#rows' => 15,
|
| 31 |
'#title' => t('Computed Code'),
|
| 32 |
'#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>',
|
| 33 |
'#default_value' => !empty($field['code']) ? $field['code'] : '$node_field[0][\'value\'] = "";',
|
| 34 |
);
|
| 35 |
$form['display'] = array(
|
| 36 |
'#type' => 'checkbox',
|
| 37 |
'#title' => t('Display this field'),
|
| 38 |
'#default_value' => is_numeric($field['code']) ? $field['code'] : TRUE ,
|
| 39 |
);
|
| 40 |
$form['display_format'] = array(
|
| 41 |
'#type' => 'textarea',
|
| 42 |
'#title' => t('Display Format'),
|
| 43 |
'#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.'),
|
| 44 |
'#default_value' => !empty($field['display_format']) ? $field['display_format'] : '$display = $node_field_item[\'value\'];',
|
| 45 |
);
|
| 46 |
$form['store'] = array(
|
| 47 |
'#type' => 'checkbox',
|
| 48 |
'#title' => t('Store using the database settings below'),
|
| 49 |
'#default_value' => is_numeric($field['store']) ? $field['store'] : 1 ,
|
| 50 |
);
|
| 51 |
$form['database'] = array('#type' => 'fieldset', '#title' => t('Database Storage Settings'));
|
| 52 |
$form['database']['data_type'] = array(
|
| 53 |
'#type' => 'radios',
|
| 54 |
'#title' => t('Data Type'),
|
| 55 |
'#description' => t('The SQL datatype to store this field in.'),
|
| 56 |
'#default_value' => !empty($field['data_type']) ? $field['data_type'] : 'varchar',
|
| 57 |
'#options' => array('int' => 'int', 'float' => 'float', 'varchar' => 'varchar', 'text' => 'text', 'longtext' => 'longtext'),
|
| 58 |
'#required' => FALSE,
|
| 59 |
);
|
| 60 |
$form['database']['data_length'] = array(
|
| 61 |
'#type' => 'textfield',
|
| 62 |
'#title' => t('Data Length'),
|
| 63 |
'#default_value' => !empty($field['data_length']) ? $field['data_length'] : NULL,
|
| 64 |
'#required' => FALSE,
|
| 65 |
);
|
| 66 |
$form['database']['data_default'] = array(
|
| 67 |
'#type' => 'textfield',
|
| 68 |
'#title' => t('Default Value'),
|
| 69 |
'#default_value' => $field['data_default'],
|
| 70 |
'#required' => FALSE,
|
| 71 |
);
|
| 72 |
$form['database']['data_not_NULL'] = array(
|
| 73 |
'#type' => 'checkbox',
|
| 74 |
'#title' => t('Not NULL'),
|
| 75 |
'#default_value' => is_numeric($field['data_not_NULL']) ? $field['data_not_NULL'] : FALSE,
|
| 76 |
);
|
| 77 |
$form['database']['data_sortable'] = array(
|
| 78 |
'#type' => 'checkbox',
|
| 79 |
'#title' => t('Sortable'),
|
| 80 |
'#default_value' => is_numeric($field['data_sortable']) ? $field['data_sortable'] : TRUE,
|
| 81 |
);
|
| 82 |
return $form;
|
| 83 |
case 'validate':
|
| 84 |
if ($field['store']) {
|
| 85 |
if (empty($field['data_type'])) {
|
| 86 |
form_set_error('data_type', t('To store this field in the database, please specify a data type.'));
|
| 87 |
}
|
| 88 |
if (!($field['data_type'] == 'text' || $field['data_type'] == 'longtext') && empty($field['data_length'])) {
|
| 89 |
form_set_error('data_length', t('To store this field in the database, please specify the data length.'));
|
| 90 |
}
|
| 91 |
}
|
| 92 |
break;
|
| 93 |
case 'save':
|
| 94 |
return array('code', 'display', 'display_format', 'store', 'data_type', 'data_length', 'data_not_NULL', 'data_default', 'data_sortable');
|
| 95 |
case 'database columns':
|
| 96 |
if ($field['store']) {
|
| 97 |
$columns = array('value' => array());
|
| 98 |
if ($field['data_type'] == 'longtext') {
|
| 99 |
$columns['value']['type'] = 'text';
|
| 100 |
$columns['value']['size'] = 'big';
|
| 101 |
}
|
| 102 |
else {
|
| 103 |
$columns['value']['type'] = isset($field['data_type']) ? $field['data_type'] : 'varchar';
|
| 104 |
}
|
| 105 |
// text and longtext should not have a length, so we ignore it
|
| 106 |
if (!($field['data_type'] == 'text' || $field['data_type'] == 'longtext')) {
|
| 107 |
$columns['value']['length'] = isset($field['data_length']) ? $field['data_length'] : 32;
|
| 108 |
}
|
| 109 |
$columns['value']['not NULL'] = isset($field['data_not_NULL']) ? $field['data_not_NULL'] : TRUE;
|
| 110 |
$columns['value']['sortable'] = isset($field['data_sortable']) ? $field['data_sortable'] : FALSE;
|
| 111 |
if ($field['data_default'] != '') {
|
| 112 |
$columns['value']['default'] = $field['data_default'];
|
| 113 |
}
|
| 114 |
}
|
| 115 |
return $columns;
|
| 116 |
|
| 117 |
case 'filters':
|
| 118 |
return array(
|
| 119 |
'default' => array(
|
| 120 |
'name' => t('Default'),
|
| 121 |
'operator' => 'views_handler_operator_gtlt',
|
| 122 |
),
|
| 123 |
);
|
| 124 |
case 'callbacks':
|
| 125 |
return array(
|
| 126 |
'view' => CONTENT_CALLBACK_CUSTOM,
|
| 127 |
);
|
| 128 |
case 'views data':
|
| 129 |
$allowed_values = content_allowed_values($field);
|
| 130 |
if (count($allowed_values)) {
|
| 131 |
$data = content_views_field_views_data($field);
|
| 132 |
$db_info = content_database_info($field);
|
| 133 |
$table_alias = content_views_tablename($field);
|
| 134 |
|
| 135 |
// Swap the filter handler to the 'in' operator.
|
| 136 |
$data[$table_alias][$field['field_name'] .'_value']['filter']['handler'] = 'content_handler_filter_many_to_one';
|
| 137 |
return $data;
|
| 138 |
}
|
| 139 |
}
|
| 140 |
}
|
| 141 |
|
| 142 |
function _computed_field_compute_value(&$node, $field, &$node_field) {
|
| 143 |
if (isset($field['code'])) {
|
| 144 |
eval($field['code']);
|
| 145 |
}
|
| 146 |
}
|
| 147 |
|
| 148 |
/**
|
| 149 |
* Implementation of cck hook_field()
|
| 150 |
*/
|
| 151 |
function computed_field_field($op, &$node, $field, &$node_field, $teaser, $page) {
|
| 152 |
switch ($op) {
|
| 153 |
case 'load':
|
| 154 |
// compute field on load if it isn't stored in the database
|
| 155 |
if (!$field['store']) {
|
| 156 |
_computed_field_compute_value($node, $field, $node_field);
|
| 157 |
return array($field['field_name'] => $node_field);
|
| 158 |
}
|
| 159 |
break;
|
| 160 |
case 'sanitize':
|
| 161 |
// compute field for node previews
|
| 162 |
if ($node->build_mode == NODE_BUILD_PREVIEW) {
|
| 163 |
_computed_field_compute_value($node, $field, $node_field);
|
| 164 |
}
|
| 165 |
break;
|
| 166 |
case 'view':
|
| 167 |
if ($field['display']) {
|
| 168 |
$items = array();
|
| 169 |
foreach ($node_field as $delta => $item) {
|
| 170 |
$items[$delta]['view'] = content_format($field, $item, 'default', $node);
|
| 171 |
}
|
| 172 |
return theme('field', $node, $field, $items, $teaser, $page);
|
| 173 |
}
|
| 174 |
break;
|
| 175 |
case 'validate':
|
| 176 |
break;
|
| 177 |
case 'insert':
|
| 178 |
case 'update':
|
| 179 |
_computed_field_compute_value($node, $field, $node_field);
|
| 180 |
break;
|
| 181 |
}
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
| 185 |
* Implementation of cck hook_widget_info()
|
| 186 |
*/
|
| 187 |
function computed_field_widget_info() {
|
| 188 |
return array(
|
| 189 |
'computed' => array(
|
| 190 |
'label' => t('Computed'),
|
| 191 |
'field types' => array('computed'),
|
| 192 |
'multiple values' => CONTENT_HANDLE_MODULE,
|
| 193 |
),
|
| 194 |
);
|
| 195 |
}
|
| 196 |
|
| 197 |
/**
|
| 198 |
* Implementation of cck hook_widget()
|
| 199 |
*/
|
| 200 |
function computed_field_widget(&$form, &$form_state, $field, $items, $delta = 0) {
|
| 201 |
$elements = array();
|
| 202 |
foreach($items as $delta => $item) {
|
| 203 |
$elements[$delta]['value'] = array (
|
| 204 |
'#type' => 'value',
|
| 205 |
'#tree' => TRUE,
|
| 206 |
'#default_value' => isset($item['value']) ? $item['value'] : NULL,
|
| 207 |
);
|
| 208 |
}
|
| 209 |
return $elements;
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Implementation of cck hook_view_item (obsolete, retained for backward compatibility with older cck)
|
| 214 |
*/
|
| 215 |
function computed_field_view_item($field, $node_field_item, $node = NULL) {
|
| 216 |
global $base_url;
|
| 217 |
if ($field['display']) {
|
| 218 |
$display = '';
|
| 219 |
eval($field['display_format']);
|
| 220 |
return $display;
|
| 221 |
}
|
| 222 |
}
|
| 223 |
|
| 224 |
/**
|
| 225 |
* Implementation of hook_theme()
|
| 226 |
*/
|
| 227 |
function computed_field_theme() {
|
| 228 |
return array(
|
| 229 |
'computed_field_formatter_default' => array(
|
| 230 |
'arguments' => array('element' => NULL),
|
| 231 |
),
|
| 232 |
'computed_field_formatter_plain' => array(
|
| 233 |
'arguments' => array('element' => NULL),
|
| 234 |
),
|
| 235 |
'computed_field_formatter_markup' => array(
|
| 236 |
'arguments' => array('element' => NULL),
|
| 237 |
),
|
| 238 |
'computed_field_formatter_computed_value' => array(
|
| 239 |
'arguments' => array('element' => NULL),
|
| 240 |
),
|
| 241 |
);
|
| 242 |
}
|
| 243 |
|
| 244 |
/**
|
| 245 |
* Implementation of cck hook_field_formatter_info()
|
| 246 |
*/
|
| 247 |
function computed_field_field_formatter_info() {
|
| 248 |
return array(
|
| 249 |
'default' => array(
|
| 250 |
'label' => t('Raw text'),
|
| 251 |
'field types' => array('computed'),
|
| 252 |
),
|
| 253 |
'plain' => array(
|
| 254 |
'label' => t('Plain text'),
|
| 255 |
'field types' => array('computed'),
|
| 256 |
),
|
| 257 |
'markup' => array(
|
| 258 |
'label' => t('Markup'),
|
| 259 |
'field types' => array('computed'),
|
| 260 |
),
|
| 261 |
'computed_value' => array(
|
| 262 |
'label' => t('Computed Value'),
|
| 263 |
'field types' => array('computed'),
|
| 264 |
),
|
| 265 |
);
|
| 266 |
}
|
| 267 |
|
| 268 |
/**
|
| 269 |
* Theme function for 'default' text field formatter.
|
| 270 |
*/
|
| 271 |
function theme_computed_field_formatter_default($element) {
|
| 272 |
$field = content_fields($element['#field_name']);
|
| 273 |
// For "some" backwards compatibility
|
| 274 |
$node_field_item['value'] = $element['#item']['value'];
|
| 275 |
eval($field['display_format']);
|
| 276 |
return $display;
|
| 277 |
}
|
| 278 |
|
| 279 |
/**
|
| 280 |
* Theme function for 'plain' text field formatter.
|
| 281 |
*/
|
| 282 |
function theme_computed_field_formatter_plain($element) {
|
| 283 |
$field = content_fields($element['#field_name']);
|
| 284 |
// For "some" backwards compatibility
|
| 285 |
$node_field_item['value'] = $element['#item']['value'];
|
| 286 |
eval($field['display_format']);
|
| 287 |
return check_plain($display);
|
| 288 |
}
|
| 289 |
|
| 290 |
/**
|
| 291 |
* Theme function for 'markup' text field formatter.
|
| 292 |
*/
|
| 293 |
function theme_computed_field_formatter_markup($element) {
|
| 294 |
$field = content_fields($element['#field_name']);
|
| 295 |
// For "some" backwards compatibility
|
| 296 |
$node_field_item['value'] = $element['#item']['value'];
|
| 297 |
eval($field['display_format']);
|
| 298 |
return check_markup($display);
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* Theme function for 'computed_value' text field formatter.
|
| 303 |
*/
|
| 304 |
function theme_computed_field_formatter_computed_value($element) {
|
| 305 |
return $element['#item']['value'];
|
| 306 |
}
|
| 307 |
|
| 308 |
/**
|
| 309 |
* Implementation of cck hook_content_is_empty().
|
| 310 |
*/
|
| 311 |
function computed_field_content_is_empty() {
|
| 312 |
return FALSE;
|
| 313 |
}
|
| 314 |
|
| 315 |
/**
|
| 316 |
* Implementation of hook_token_list()
|
| 317 |
*/
|
| 318 |
function computed_field_token_list($type = 'all') {
|
| 319 |
if ($type == 'field' || $type == 'all') {
|
| 320 |
$tokens = array();
|
| 321 |
|
| 322 |
$tokens['computed_field']['raw'] = t("Raw, unfiltered text.");
|
| 323 |
$tokens['computed_field']['formatted'] = t("Formatted and filtered text.");
|
| 324 |
|
| 325 |
return $tokens;
|
| 326 |
}
|
| 327 |
}
|
| 328 |
|
| 329 |
/**
|
| 330 |
* Implementation of hook_token_values()
|
| 331 |
*/
|
| 332 |
function computed_field_token_values($type, $object = NULL, $options = array()) {
|
| 333 |
if ($type == 'field') {
|
| 334 |
$item = $object[0];
|
| 335 |
$tokens['raw'] = $item['value'];
|
| 336 |
$tokens['formatted'] = $item['view'];
|
| 337 |
return $tokens;
|
| 338 |
}
|
| 339 |
}
|