1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Implements field hook_field_info().
|
5
|
*/
|
6
|
function computed_field_field_info() {
|
7
|
return array(
|
8
|
'computed' => array(
|
9
|
'label' => t('Computed'),
|
10
|
'description' => t('Create field data via PHP code.'),
|
11
|
'settings' => array(
|
12
|
'code' => '$entity_field[0][\'value\'] = "";',
|
13
|
'display_format' => '$display_output = $entity_field_item[\'value\'];',
|
14
|
'store' => 1,
|
15
|
'database' => array(
|
16
|
'data_type' => 'varchar',
|
17
|
'data_length' => 32,
|
18
|
'data_size' => 'normal',
|
19
|
'data_precision' => 10,
|
20
|
'data_scale' => 2,
|
21
|
'data_not_NULL' => FALSE,
|
22
|
'data_default' => NULL,
|
23
|
'data_index' => FALSE,
|
24
|
),
|
25
|
),
|
26
|
'default_widget' => 'computed',
|
27
|
'default_formatter' => 'computed_field_plain',
|
28
|
// If we followed the core convention of separate fields for each data
|
29
|
// type we could make Entity API happy by just setting a property_type.
|
30
|
// Instead we have to use our own callback to determine the type then
|
31
|
// rerun theirs to setup the rest of the field properties.
|
32
|
'property_callbacks' => array('computed_field_entity_property_callback'),
|
33
|
),
|
34
|
);
|
35
|
}
|
36
|
|
37
|
/**
|
38
|
* Callback to setup Entity API's field properties.
|
39
|
*/
|
40
|
function computed_field_entity_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
|
41
|
$property_types = array(
|
42
|
'int' => 'integer',
|
43
|
'float' => 'decimal', 'numeric' => 'decimal',
|
44
|
'varchar' => 'text', 'text' => 'text', 'longtext' => 'text',
|
45
|
);
|
46
|
if (isset($field['columns']['value']) && isset($property_types[$field['columns']['value']['type']])) {
|
47
|
// Entity API's defaults are pretty good so set the property_type and let
|
48
|
// them do the work for us.
|
49
|
$field_type['property_type'] = $property_types[$field['columns']['value']['type']];
|
50
|
entity_metadata_field_default_property_callback($info, $entity_type, $field, $instance, $field_type);
|
51
|
// The only thing is that a setter doesn't make sense, so let's disable it.
|
52
|
$property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
|
53
|
unset($property['setter callback']);
|
54
|
}
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Implements of hook_field_settings_form().
|
59
|
*/
|
60
|
function computed_field_field_settings_form($field, $instance, $has_data) {
|
61
|
$form = array();
|
62
|
$compute_func = 'computed_field_' . $field['field_name'] . '_compute';
|
63
|
$display_func = 'computed_field_' . $field['field_name'] . '_display';
|
64
|
$settings = $field['settings'];
|
65
|
|
66
|
$form['#element_validate'] = array('computed_field_field_settings_form_validate');
|
67
|
|
68
|
$form['code'] = array(
|
69
|
'#type' => 'textarea',
|
70
|
'#rows' => 15,
|
71
|
'#title' => t('Computed Code (PHP)'),
|
72
|
'#description' => t('The variables available to your code include: <code>@fields</code>. To set the value of the field, set <code>@entity_field</code>. For multi-value computed fields continue with <code>@entity_field_multi</code>. Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields (<code>@field_a</code> and <code>@field_b</code>) in a node entity:<p><code>@example</code> The first pop fetches the last (or only) item from the field while the second pop fetches its <code>[\'value\']</code> contents (assuming it\'s the only key that\'s set).<p>Alternately, this code can be supplied by your own custom function named: <code>@compute_func(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items)</code>',
|
73
|
array('@fields' => '&$entity_field, $entity_type, $entity, $field, $instance, $langcode, and $items',
|
74
|
'@entity_field' => '$entity_field[0][\'value\']',
|
75
|
'@entity_field_multi' => '$entity_field[1][\'value\']',
|
76
|
'@field_a' => 'field_a',
|
77
|
'@field_b' => 'field_b',
|
78
|
'@example' => '$entity_field[0][\'value\'] = array_pop(array_pop(field_get_items($entity_type, $entity, \'field_a\'))) + array_pop(array_pop(field_get_items($entity_type, $entity, \'field_b\')));',
|
79
|
'@compute_func' => $compute_func)),
|
80
|
'#default_value' => !empty($settings['code']) ? $settings['code'] : '$entity_field[0][\'value\'] = "";',
|
81
|
'#access' => !function_exists($compute_func),
|
82
|
);
|
83
|
if (function_exists($compute_func)) {
|
84
|
$form['compute_func'] = array(
|
85
|
'#type' => 'item',
|
86
|
'#markup' => t('<strong>This field is COMPUTED using <code>@compute_func()</code>.</strong>', array('@compute_func' => $compute_func)),
|
87
|
);
|
88
|
}
|
89
|
$form['display_format'] = array(
|
90
|
'#type' => 'textarea',
|
91
|
'#title' => t('Display Code (PHP)'),
|
92
|
'#description' => t('This code should assign a string to the <code>@display_output</code> variable, which will be printed when the field is displayed. The raw computed value of the field is in <code>@value</code>. <strong>Note:</strong> this code has no effect if you use the "Raw computed value" display formatter.<p> Alternately, this code can be supplied by your own custom function named: <code>@display_func($field, $entity_field_item, $entity_lang, $langcode, $entity)</code>. Return the value to be displayed. Original value is in $entity_field_item[\'value\'].',
|
93
|
array('@display_output' => '$display_output',
|
94
|
'@value' => '$entity_field_item[\'value\']',
|
95
|
'@display_func' => $display_func)),
|
96
|
'#default_value' => !empty($settings['display_format']) ? $settings['display_format'] : '$display_output = $entity_field_item[\'value\'];',
|
97
|
'#access' => !function_exists($display_func),
|
98
|
);
|
99
|
if (function_exists($display_func)) {
|
100
|
$form['display_func'] = array(
|
101
|
'#type' => 'item',
|
102
|
'#markup' => t('<strong>This field is DISPLAYED using <code>@display_func()</code>.</strong>', array('@display_func' => $display_func)),
|
103
|
);
|
104
|
}
|
105
|
$form['store'] = array(
|
106
|
'#type' => 'checkbox',
|
107
|
'#title' => t('Store value in the database'),
|
108
|
'#description' => t('The value will be stored in the database with the settings below. As a result, it will only be recalculated when the entity is updated. This option is required when accessing the field through Views.'),
|
109
|
'#default_value' => is_numeric($settings['store']) ? $settings['store'] : 1 ,
|
110
|
'#disabled' => $has_data,
|
111
|
);
|
112
|
$form['database'] = array('#type' => 'fieldset', '#title' => t('Database Storage Settings'));
|
113
|
|
114
|
if ($has_data) {
|
115
|
$form['database']['warning'] = array(
|
116
|
'#type' => 'item',
|
117
|
'#markup' => t('<strong>**This field currently has stored data, so modifications to its DB settings are not allowed.**</strong>'),
|
118
|
);
|
119
|
}
|
120
|
$form['database']['data_type'] = array(
|
121
|
'#type' => 'radios',
|
122
|
'#title' => t('Data Type'),
|
123
|
'#description' => t('The SQL datatype to store this field in.'),
|
124
|
'#default_value' => !empty($settings['database']['data_type']) ? $settings['database']['data_type'] : 'varchar',
|
125
|
'#options' => array('varchar' => 'varchar', 'text' => 'text', 'longtext' => 'longtext', 'int' => 'int', 'float' => 'float', 'numeric' => 'decimal'),
|
126
|
'#required' => FALSE,
|
127
|
'#disabled' => $has_data,
|
128
|
);
|
129
|
$form['database']['data_length'] = array(
|
130
|
'#type' => 'textfield',
|
131
|
'#title' => t('Data Length (varchar/text)'),
|
132
|
'#description' => t('<strong>Only</strong> valid for <strong>varchar</strong> or <strong>text</strong> fields. The length of the field stored in the database.'),
|
133
|
'#default_value' => !empty($settings['database']['data_length']) ? $settings['database']['data_length'] : 32,
|
134
|
'#required' => FALSE,
|
135
|
'#disabled' => $has_data,
|
136
|
);
|
137
|
$form['database']['data_size'] = array(
|
138
|
'#type' => 'select',
|
139
|
'#title' => t('Data Size (int/float)'),
|
140
|
'#description' => t('<strong>Only</strong> valid for <strong>int</strong> or <strong>float</strong> fields. The size of the field stored in the database.'),
|
141
|
'#default_value' => !empty($settings['database']['data_size']) ? $settings['database']['data_size'] : 'normal',
|
142
|
'#options' => array('tiny' => 'tiny', 'small' => 'small', 'medium' => 'medium', 'normal' => 'normal', 'big' => 'big'),
|
143
|
'#required' => FALSE,
|
144
|
'#disabled' => $has_data,
|
145
|
);
|
146
|
$form['database']['data_precision'] = array(
|
147
|
'#type' => 'select',
|
148
|
'#title' => t('Decimal Precision (decimal)'),
|
149
|
'#description' => t('<strong>Only</strong> valid for <strong>decimal</strong> fields. The total number of digits to store in the database, including those to the right of the decimal.'),
|
150
|
'#options' => drupal_map_assoc(range(10, 32)),
|
151
|
'#default_value' => !empty($settings['database']['data_precision']) ? $settings['database']['data_precision'] : 10,
|
152
|
'#required' => FALSE,
|
153
|
'#disabled' => $has_data,
|
154
|
);
|
155
|
$form['database']['data_scale'] = array(
|
156
|
'#type' => 'select',
|
157
|
'#title' => t('Decimal Scale (decimal)'),
|
158
|
'#description' => t('<strong>Only</strong> valid for <strong>decimal</strong> fields. The number of digits to the right of the decimal. '),
|
159
|
'#options' => drupal_map_assoc(range(0, 10)),
|
160
|
'#default_value' => !empty($settings['database']['data_scale']) ? $settings['database']['data_scale'] : 2,
|
161
|
'#required' => FALSE,
|
162
|
'#disabled' => $has_data,
|
163
|
);
|
164
|
$form['database']['data_default'] = array(
|
165
|
'#type' => 'textfield',
|
166
|
'#title' => t('Default Value'),
|
167
|
'#default_value' => $settings['database']['data_default'],
|
168
|
'#required' => FALSE,
|
169
|
'#disabled' => $has_data,
|
170
|
);
|
171
|
$form['database']['data_not_NULL'] = array(
|
172
|
'#type' => 'checkbox',
|
173
|
'#title' => t('Not NULL'),
|
174
|
'#default_value' => is_numeric($settings['database']['data_not_NULL']) ? $settings['database']['data_not_NULL'] : FALSE,
|
175
|
'#disabled' => $has_data,
|
176
|
);
|
177
|
$form['database']['data_index'] = array(
|
178
|
'#type' => 'checkbox',
|
179
|
'#title' => t('Index computed values in the database (Does not apply to text or longtext fields.)'),
|
180
|
'#default_value' => is_numeric($settings['database']['data_index']) ? $settings['database']['data_index'] : FALSE,
|
181
|
'#disabled' => $has_data,
|
182
|
);
|
183
|
return $form;
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* Implements the #element_validate callback for computed_field_field_settings_form().
|
188
|
*/
|
189
|
function computed_field_field_settings_form_validate($element, &$form_state) {
|
190
|
$settings = $form_state['values']['field']['settings'];
|
191
|
if ($settings['store']) {
|
192
|
if (empty($settings['database']['data_type'])) {
|
193
|
form_set_error('field][settings][data_type', t('To store this field in the database, please specify a data type.'));
|
194
|
}
|
195
|
if (($settings['database']['data_type'] == 'text' || $settings['database']['data_type'] == 'varchar') && empty($settings['database']['data_length'])) {
|
196
|
form_set_error('field][settings][database][data_length', t('To store this field in the database, please specify the data length.'));
|
197
|
}
|
198
|
if (($settings['database']['data_type'] == 'int' || $settings['database']['data_type'] == 'float') && (!empty($settings['database']['data_default']) && !is_numeric($settings['database']['data_default']))) {
|
199
|
form_set_error('field][settings][database][data_default', t('Your default value should be numeric given your data type.'));
|
200
|
}
|
201
|
}
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Implements field hook_field_load().
|
206
|
*/
|
207
|
function computed_field_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
|
208
|
$settings = $field['settings'];
|
209
|
// Compute field values on load if they aren't stored in the database
|
210
|
if (!$settings['store']) {
|
211
|
foreach ($entities as $etid => $entity) {
|
212
|
_computed_field_compute_value($entity_type, $entity, $field, $instances, $langcode, $items[$etid]);
|
213
|
}
|
214
|
}
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* Implements field hook_field_prepare_view().
|
219
|
*/
|
220
|
function computed_field_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
|
221
|
// Compute field values in case user is "previewing" an entity
|
222
|
foreach ($entities as $etid => $entity) {
|
223
|
if (isset($entity->op) && $entity->op == 'Preview') {
|
224
|
_computed_field_compute_value($entity_type, $entity, $field, $instances, $langcode, $items[$etid]);
|
225
|
}
|
226
|
}
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Implements field hook_field_insert().
|
231
|
*/
|
232
|
function computed_field_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
233
|
_computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, $items);
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Implements field hook_field_update().
|
238
|
*/
|
239
|
function computed_field_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
240
|
_computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, $items);
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* Implements field hook_field_widget_info().
|
245
|
*/
|
246
|
function computed_field_field_widget_info() {
|
247
|
return array(
|
248
|
'computed' => array(
|
249
|
'label' => t('Computed'),
|
250
|
'field types' => array('computed'),
|
251
|
'behaviors' => array(
|
252
|
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
|
253
|
'default value' => FIELD_BEHAVIOR_NONE,
|
254
|
),
|
255
|
),
|
256
|
);
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* Implements field hook_field_widget_form().
|
261
|
*/
|
262
|
|
263
|
function computed_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
264
|
|
265
|
// If there are no items yet, add a null item value to avoid
|
266
|
// preview errors when selecting a different language
|
267
|
if (empty($items)) $items[0]['value'] = NULL;
|
268
|
|
269
|
foreach ($items as $item_delta => $item) {
|
270
|
$element[$item_delta]['value'] = array(
|
271
|
'#type' => 'value',
|
272
|
'#tree' => TRUE,
|
273
|
'#default_value' => isset($item['value']) ? $item['value'] : NULL,
|
274
|
);
|
275
|
}
|
276
|
return $element;
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* Implements hook_field_formatter_info().
|
281
|
*/
|
282
|
function computed_field_field_formatter_info() {
|
283
|
return array(
|
284
|
'computed_field_unsanitized' => array(
|
285
|
'label' => t('Unsanitized'),
|
286
|
'field types' => array('computed'),
|
287
|
),
|
288
|
'computed_field_plain' => array(
|
289
|
'label' => t('Plain text'),
|
290
|
'field types' => array('computed'),
|
291
|
),
|
292
|
'computed_field_markup' => array(
|
293
|
'label' => t('Filtered markup'),
|
294
|
'field types' => array('computed'),
|
295
|
),
|
296
|
'computed_field_computed_value' => array(
|
297
|
'label' => t('Raw value, no display code'),
|
298
|
'field types' => array('computed'),
|
299
|
),
|
300
|
);
|
301
|
}
|
302
|
|
303
|
/**
|
304
|
* Implements hook_field_formatter_view().
|
305
|
*/
|
306
|
function computed_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
|
307
|
$element = array();
|
308
|
|
309
|
// Special case formatter that returns the raw computed values without any display code processing
|
310
|
if ($display['type'] == "computed_field_computed_value") {
|
311
|
foreach ($items as $delta => $item) {
|
312
|
if (!isset($entity_field_item['value'])) $entity_field_item['value'] = NULL;
|
313
|
$element[$delta] = array('#markup' => $item['value']);
|
314
|
}
|
315
|
return $element;
|
316
|
}
|
317
|
|
318
|
// Other display formatters which run through display code processing
|
319
|
// Check if the value is to be formatted by a display function outside the DB
|
320
|
$display_func = 'computed_field_' . $field['field_name'] . '_display';
|
321
|
if (function_exists($display_func)) $display_in_code = TRUE;
|
322
|
else $display_in_code = FALSE;
|
323
|
|
324
|
// Loop the items to display
|
325
|
foreach ($items as $delta => $item) {
|
326
|
|
327
|
// For "some" backwards compatibility
|
328
|
$entity_field_item = $item;
|
329
|
|
330
|
// Setup a variable with the entity language if available
|
331
|
if (isset($entity->language)) $entity_lang = $entity->language;
|
332
|
else $entity_lang = LANGUAGE_NONE;
|
333
|
|
334
|
// If there are value "holes" in the field array let's set the value to NULL
|
335
|
// to avoid undefined index errors in typical PHP display code
|
336
|
if (!isset($entity_field_item['value'])) $entity_field_item['value'] = NULL;
|
337
|
|
338
|
// Execute the display code
|
339
|
$display_output = NULL;
|
340
|
if ($display_in_code) {
|
341
|
$display_output = $display_func($field, $entity_field_item, $entity_lang, $langcode, $entity);
|
342
|
}
|
343
|
else {
|
344
|
eval($field['settings']['display_format']);
|
345
|
}
|
346
|
|
347
|
// Output the formatted display item
|
348
|
switch ($display['type']) {
|
349
|
case 'computed_field_unsanitized':
|
350
|
$element[$delta] = array('#markup' => $display_output);
|
351
|
break;
|
352
|
case 'computed_field_plain':
|
353
|
$element[$delta] = array('#markup' => check_plain($display_output));
|
354
|
break;
|
355
|
case 'computed_field_markup':
|
356
|
$element[$delta] = array('#markup' => check_markup($display_output));
|
357
|
break;
|
358
|
}
|
359
|
}
|
360
|
return $element;
|
361
|
}
|
362
|
|
363
|
/**
|
364
|
* Implements field hook_field_is_empty().
|
365
|
*/
|
366
|
function computed_field_field_is_empty($item, $field) {
|
367
|
$data_type = $field['settings']['database']['data_type'];
|
368
|
if ($data_type == 'int' || $data_type == 'float') {
|
369
|
return !is_numeric($item['value']);
|
370
|
}
|
371
|
return empty($item['value']);
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* Private function to compute the fields value.
|
376
|
*/
|
377
|
function _computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
378
|
$settings = $field['settings'];
|
379
|
|
380
|
// Setup a variable with the field values
|
381
|
$entity_field =& $items;
|
382
|
|
383
|
// Setup a variable with the entity language if available
|
384
|
if (isset($entity->language)) $entity_lang = $entity->language;
|
385
|
else $entity_lang = LANGUAGE_NONE;
|
386
|
|
387
|
// Allow the value to be computed from code not stored in DB
|
388
|
$compute_func = 'computed_field_' . $field['field_name'] . '_compute';
|
389
|
if (function_exists($compute_func)) {
|
390
|
$compute_func($entity_field, $entity_type, $entity, $field, $instance, $langcode, $items);
|
391
|
}
|
392
|
else {
|
393
|
if (isset($settings['code'])) {
|
394
|
eval($settings['code']);
|
395
|
}
|
396
|
}
|
397
|
}
|
398
|
|