1 |
1 |
<?php
|
2 |
2 |
|
3 |
3 |
/**
|
4 |
|
* Implements field hook_field_info().
|
|
4 |
* @file
|
|
5 |
* Functionality for the computed field.
|
|
6 |
*/
|
|
7 |
|
|
8 |
/**
|
|
9 |
* Implements hook_field_info().
|
5 |
10 |
*/
|
6 |
11 |
function computed_field_field_info() {
|
7 |
12 |
return array(
|
... | ... | |
12 |
17 |
'code' => '$entity_field[0][\'value\'] = "";',
|
13 |
18 |
'display_format' => '$display_output = $entity_field_item[\'value\'];',
|
14 |
19 |
'store' => 1,
|
|
20 |
'recalculate' => FALSE,
|
15 |
21 |
'database' => array(
|
16 |
22 |
'data_type' => 'varchar',
|
17 |
23 |
'data_length' => 32,
|
... | ... | |
40 |
46 |
function computed_field_entity_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
|
41 |
47 |
$property_types = array(
|
42 |
48 |
'int' => 'integer',
|
43 |
|
'float' => 'decimal', 'numeric' => 'decimal',
|
44 |
|
'varchar' => 'text', 'text' => 'text', 'longtext' => 'text',
|
|
49 |
'float' => 'decimal',
|
|
50 |
'numeric' => 'decimal',
|
|
51 |
'varchar' => 'text',
|
|
52 |
'text' => 'text',
|
|
53 |
'longtext' => 'text',
|
45 |
54 |
);
|
46 |
55 |
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.
|
|
56 |
// Entity API's defaults are pretty good so set the property_type and let them do the work for us.
|
49 |
57 |
$field_type['property_type'] = $property_types[$field['columns']['value']['type']];
|
50 |
58 |
entity_metadata_field_default_property_callback($info, $entity_type, $field, $instance, $field_type);
|
51 |
59 |
// The only thing is that a setter doesn't make sense, so let's disable it.
|
... | ... | |
55 |
63 |
}
|
56 |
64 |
|
57 |
65 |
/**
|
58 |
|
* Implements of hook_field_settings_form().
|
|
66 |
* Implements hook_field_settings_form().
|
59 |
67 |
*/
|
60 |
68 |
function computed_field_field_settings_form($field, $instance, $has_data) {
|
61 |
69 |
$form = array();
|
... | ... | |
69 |
77 |
'#type' => 'textarea',
|
70 |
78 |
'#rows' => 15,
|
71 |
79 |
'#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 |
'#description' => t('<p>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> !example <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>.</p>', array(
|
|
81 |
'@fields' => '&$entity_field, $entity_type, $entity, $field, $instance, $langcode, and $items',
|
|
82 |
'@entity_field' => '$entity_field[0][\'value\']',
|
|
83 |
'@entity_field_multi' => '$entity_field[1][\'value\']',
|
|
84 |
'@field_a' => 'field_a',
|
|
85 |
'@field_b' => 'field_b',
|
|
86 |
'!example' => '<p><code>$field_a = field_get_items($entity_type, $entity, "field_a");<br />
|
|
87 |
$field_b = field_get_items($entity_type, $entity, "field_b");<br />
|
|
88 |
$entity_field[0]["value"] = $field_a[0]["value"] + $field_b[0]["value"];</code></p>
|
|
89 |
',
|
|
90 |
'@compute_func' => $compute_func,
|
|
91 |
)),
|
80 |
92 |
'#default_value' => !empty($settings['code']) ? $settings['code'] : '$entity_field[0][\'value\'] = "";',
|
81 |
93 |
'#access' => !function_exists($compute_func),
|
82 |
94 |
);
|
... | ... | |
89 |
101 |
$form['display_format'] = array(
|
90 |
102 |
'#type' => 'textarea',
|
91 |
103 |
'#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)),
|
|
104 |
'#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\'].', array(
|
|
105 |
'@display_output' => '$display_output',
|
|
106 |
'@value' => '$entity_field_item[\'value\']',
|
|
107 |
'@display_func' => $display_func,
|
|
108 |
)),
|
96 |
109 |
'#default_value' => !empty($settings['display_format']) ? $settings['display_format'] : '$display_output = $entity_field_item[\'value\'];',
|
97 |
110 |
'#access' => !function_exists($display_func),
|
98 |
111 |
);
|
... | ... | |
100 |
113 |
$form['display_func'] = array(
|
101 |
114 |
'#type' => 'item',
|
102 |
115 |
'#markup' => t('<strong>This field is DISPLAYED using <code>@display_func()</code>.</strong>', array('@display_func' => $display_func)),
|
103 |
|
);
|
|
116 |
);
|
104 |
117 |
}
|
|
118 |
$form['recalculate'] = array(
|
|
119 |
'#type' => 'checkbox',
|
|
120 |
'#title' => t('Recalculate the field value every time.'),
|
|
121 |
'#description' => t('By default, Drupal will cache the value of this field even if it is not stored in the database (and even if Page Caching is disabled). This option will cause computed_field to recalculate the value every time this field is displayed. For example, a time-based calculated value may change more often than field cache is cleared. (Note that Drupal page caching will still cache the field value.)'),
|
|
122 |
'#default_value' => is_numeric($settings['recalculate']) ? $settings['recalculate'] : FALSE,
|
|
123 |
);
|
105 |
124 |
$form['store'] = array(
|
106 |
125 |
'#type' => 'checkbox',
|
107 |
126 |
'#title' => t('Store value in the database'),
|
... | ... | |
122 |
141 |
'#title' => t('Data Type'),
|
123 |
142 |
'#description' => t('The SQL datatype to store this field in.'),
|
124 |
143 |
'#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'),
|
|
144 |
'#options' => array(
|
|
145 |
'varchar' => 'varchar',
|
|
146 |
'text' => 'text',
|
|
147 |
'longtext' => 'longtext',
|
|
148 |
'int' => 'int',
|
|
149 |
'float' => 'float',
|
|
150 |
'numeric' => 'decimal',
|
|
151 |
),
|
126 |
152 |
'#required' => FALSE,
|
127 |
153 |
'#disabled' => $has_data,
|
128 |
154 |
);
|
... | ... | |
139 |
165 |
'#title' => t('Data Size (int/float)'),
|
140 |
166 |
'#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 |
167 |
'#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'),
|
|
168 |
'#options' => array(
|
|
169 |
'tiny' => 'tiny',
|
|
170 |
'small' => 'small',
|
|
171 |
'medium' => 'medium',
|
|
172 |
'normal' => 'normal',
|
|
173 |
'big' => 'big',
|
|
174 |
),
|
143 |
175 |
'#required' => FALSE,
|
144 |
176 |
'#disabled' => $has_data,
|
145 |
177 |
);
|
... | ... | |
184 |
216 |
}
|
185 |
217 |
|
186 |
218 |
/**
|
187 |
|
* Implements the #element_validate callback for computed_field_field_settings_form().
|
188 |
|
*/
|
|
219 |
* #element_validate callback for computed_field_field_settings_form().
|
|
220 |
*/
|
189 |
221 |
function computed_field_field_settings_form_validate($element, &$form_state) {
|
190 |
222 |
$settings = $form_state['values']['field']['settings'];
|
191 |
223 |
if ($settings['store']) {
|
... | ... | |
202 |
234 |
}
|
203 |
235 |
|
204 |
236 |
/**
|
205 |
|
* Implements field hook_field_load().
|
|
237 |
* Implements hook_field_load().
|
206 |
238 |
*/
|
207 |
239 |
function computed_field_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
|
208 |
240 |
$settings = $field['settings'];
|
209 |
|
// Compute field values on load if they aren't stored in the database
|
|
241 |
// Compute field values on load if they aren't stored in the database.
|
210 |
242 |
if (!$settings['store']) {
|
211 |
243 |
foreach ($entities as $etid => $entity) {
|
212 |
244 |
_computed_field_compute_value($entity_type, $entity, $field, $instances, $langcode, $items[$etid]);
|
... | ... | |
215 |
247 |
}
|
216 |
248 |
|
217 |
249 |
/**
|
218 |
|
* Implements field hook_field_prepare_view().
|
|
250 |
* Implements hook_field_prepare_view().
|
219 |
251 |
*/
|
220 |
252 |
function computed_field_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
|
221 |
|
// Compute field values in case user is "previewing" an entity
|
|
253 |
$settings = $field['settings'];
|
|
254 |
// Compute field values in case user is "previewing" an entity.
|
222 |
255 |
foreach ($entities as $etid => $entity) {
|
223 |
|
if (isset($entity->op) && $entity->op == 'Preview') {
|
|
256 |
if ((isset($entity->op) && $entity->op == 'Preview') || $settings['recalculate']) {
|
224 |
257 |
_computed_field_compute_value($entity_type, $entity, $field, $instances, $langcode, $items[$etid]);
|
225 |
258 |
}
|
226 |
259 |
}
|
227 |
260 |
}
|
228 |
261 |
|
229 |
262 |
/**
|
230 |
|
* Implements field hook_field_insert().
|
|
263 |
* Implements hook_field_insert().
|
231 |
264 |
*/
|
232 |
265 |
function computed_field_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
233 |
266 |
_computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, $items);
|
234 |
267 |
}
|
235 |
268 |
|
236 |
269 |
/**
|
237 |
|
* Implements field hook_field_update().
|
|
270 |
* Implements hook_field_update().
|
238 |
271 |
*/
|
239 |
272 |
function computed_field_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
240 |
273 |
_computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, $items);
|
241 |
274 |
}
|
242 |
275 |
|
243 |
276 |
/**
|
244 |
|
* Implements field hook_field_widget_info().
|
|
277 |
* Implements hook_field_widget_info().
|
245 |
278 |
*/
|
246 |
279 |
function computed_field_field_widget_info() {
|
247 |
280 |
return array(
|
... | ... | |
257 |
290 |
}
|
258 |
291 |
|
259 |
292 |
/**
|
260 |
|
* Implements field hook_field_widget_form().
|
|
293 |
* Implements hook_field_widget_form().
|
261 |
294 |
*/
|
262 |
|
|
263 |
295 |
function computed_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
264 |
296 |
|
265 |
297 |
// 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;
|
|
298 |
// preview errors when selecting a different language.
|
|
299 |
if (empty($items)) {
|
|
300 |
$items[0]['value'] = NULL;
|
|
301 |
}
|
268 |
302 |
|
269 |
303 |
foreach ($items as $item_delta => $item) {
|
270 |
304 |
$element[$item_delta]['value'] = array(
|
... | ... | |
305 |
339 |
*/
|
306 |
340 |
function computed_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
|
307 |
341 |
$element = array();
|
|
342 |
$data_to_display = FALSE;
|
308 |
343 |
|
309 |
|
// Special case formatter that returns the raw computed values without any display code processing
|
|
344 |
// Special case formatter that returns the raw computed values without any display code processing.
|
310 |
345 |
if ($display['type'] == "computed_field_computed_value") {
|
311 |
346 |
foreach ($items as $delta => $item) {
|
312 |
|
if (!isset($entity_field_item['value'])) $entity_field_item['value'] = NULL;
|
|
347 |
if (!isset($entity_field_item['value'])) {
|
|
348 |
$entity_field_item['value'] = NULL;
|
|
349 |
}
|
313 |
350 |
$element[$delta] = array('#markup' => $item['value']);
|
314 |
351 |
}
|
315 |
352 |
return $element;
|
316 |
353 |
}
|
317 |
354 |
|
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
|
|
355 |
// Other display formatters which run through display code processing.
|
|
356 |
// Check if the value is to be formatted by a display function outside the DB.
|
320 |
357 |
$display_func = 'computed_field_' . $field['field_name'] . '_display';
|
321 |
|
if (function_exists($display_func)) $display_in_code = TRUE;
|
322 |
|
else $display_in_code = FALSE;
|
|
358 |
$display_in_code = function_exists($display_func) ? TRUE : FALSE;
|
323 |
359 |
|
324 |
|
// Loop the items to display
|
|
360 |
// Loop the items to display.
|
325 |
361 |
foreach ($items as $delta => $item) {
|
326 |
362 |
|
327 |
|
// For "some" backwards compatibility
|
|
363 |
// For "some" backwards compatibility.
|
328 |
364 |
$entity_field_item = $item;
|
329 |
365 |
|
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;
|
|
366 |
// Setup a variable with the entity language if available.
|
|
367 |
if (isset($entity->language)) {
|
|
368 |
$entity_lang = $entity->language;
|
|
369 |
}
|
|
370 |
else {
|
|
371 |
$entity_lang = LANGUAGE_NONE;
|
|
372 |
}
|
333 |
373 |
|
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;
|
|
374 |
// If there are value "holes" in the field array let's set the value to NULL.
|
|
375 |
// to avoid undefined index errors in typical PHP display code.
|
|
376 |
if (!isset($entity_field_item['value'])) {
|
|
377 |
$entity_field_item['value'] = NULL;
|
|
378 |
}
|
337 |
379 |
|
338 |
|
// Execute the display code
|
|
380 |
// Execute the display code.
|
339 |
381 |
$display_output = NULL;
|
340 |
382 |
if ($display_in_code) {
|
341 |
383 |
$display_output = $display_func($field, $entity_field_item, $entity_lang, $langcode, $entity);
|
... | ... | |
344 |
386 |
eval($field['settings']['display_format']);
|
345 |
387 |
}
|
346 |
388 |
|
347 |
|
// Output the formatted display item
|
|
389 |
// Track if any of our items produce non-empty output.
|
|
390 |
if (!empty($display_output) || is_numeric($display_output)) {
|
|
391 |
$data_to_display = TRUE;
|
|
392 |
}
|
|
393 |
|
|
394 |
// Output the formatted display item.
|
348 |
395 |
switch ($display['type']) {
|
349 |
396 |
case 'computed_field_unsanitized':
|
350 |
397 |
$element[$delta] = array('#markup' => $display_output);
|
351 |
398 |
break;
|
|
399 |
|
352 |
400 |
case 'computed_field_plain':
|
353 |
401 |
$element[$delta] = array('#markup' => check_plain($display_output));
|
354 |
402 |
break;
|
|
403 |
|
355 |
404 |
case 'computed_field_markup':
|
356 |
405 |
$element[$delta] = array('#markup' => check_markup($display_output));
|
357 |
406 |
break;
|
358 |
407 |
}
|
359 |
408 |
}
|
|
409 |
// If all items are empty then we should not return anything. This helps
|
|
410 |
// ensure that empty fields are not displayed at all. This check does not
|
|
411 |
// apply to fields stored in the DB as those are instead checked on save.
|
|
412 |
if (isset($field['settings']['store']) && !$field['settings']['store'] && !$data_to_display) {
|
|
413 |
return;
|
|
414 |
}
|
360 |
415 |
return $element;
|
361 |
416 |
}
|
362 |
417 |
|
363 |
418 |
/**
|
364 |
|
* Implements field hook_field_is_empty().
|
|
419 |
* Implements hook_field_is_empty().
|
365 |
420 |
*/
|
366 |
421 |
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']);
|
|
422 |
unset($empty);
|
|
423 |
|
|
424 |
// This will depend on the class of data type.
|
|
425 |
switch ($field['settings']['database']['data_type']) {
|
|
426 |
|
|
427 |
case 'int':
|
|
428 |
case 'float':
|
|
429 |
case 'numeric':
|
|
430 |
// For numbers, the field is empty if the value isn't numeric.
|
|
431 |
$empty = !is_numeric($item['value']);
|
|
432 |
break;
|
|
433 |
|
|
434 |
case 'varchar':
|
|
435 |
case 'text':
|
|
436 |
case 'longtext':
|
|
437 |
// For strings, the field is empty if it doesn't match the empty string.
|
|
438 |
$empty = ($item['value'] === "");
|
|
439 |
break;
|
370 |
440 |
}
|
371 |
|
return empty($item['value']);
|
|
441 |
return $empty;
|
372 |
442 |
}
|
373 |
443 |
|
374 |
444 |
/**
|
... | ... | |
377 |
447 |
function _computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
378 |
448 |
$settings = $field['settings'];
|
379 |
449 |
|
380 |
|
// Setup a variable with the field values
|
|
450 |
// Setup a variable with the field values.
|
381 |
451 |
$entity_field =& $items;
|
382 |
452 |
|
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;
|
|
453 |
// Setup a variable with the entity language if available.
|
|
454 |
if (isset($entity->language)) {
|
|
455 |
$entity_lang = $entity->language;
|
|
456 |
}
|
|
457 |
else {
|
|
458 |
$entity_lang = LANGUAGE_NONE;
|
|
459 |
}
|
386 |
460 |
|
387 |
|
// Allow the value to be computed from code not stored in DB
|
|
461 |
// Allow the value to be computed from code not stored in DB.
|
388 |
462 |
$compute_func = 'computed_field_' . $field['field_name'] . '_compute';
|
389 |
463 |
if (function_exists($compute_func)) {
|
390 |
464 |
$compute_func($entity_field, $entity_type, $entity, $field, $instance, $langcode, $items);
|
... | ... | |
395 |
469 |
}
|
396 |
470 |
}
|
397 |
471 |
}
|
398 |
|
|
Weekly update of contrib modules