list.inc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @file
  4. * Provide diff field functions for the List module.
  5. */
  6. /**
  7. * Diff field callback for parsing list field comparative values.
  8. */
  9. function list_field_diff_view($items, $context) {
  10. $field = $context['field'];
  11. $instance = $context['instance'];
  12. $settings = $context['settings'];
  13. $diff_items = array();
  14. $allowed_values = list_allowed_values($field, $instance, $context['entity_type'], $context['entity']);
  15. foreach ($items as $delta => $item) {
  16. // Fairly complex condition to prevent duplicate "key (key)" type rendering.
  17. if (isset($allowed_values[$item['value']]) &&
  18. $allowed_values[$item['value']] != $item['value'] &&
  19. strlen($allowed_values[$item['value']])) {
  20. switch ($settings['compare']) {
  21. case 'both':
  22. $diff_items[$delta] = $allowed_values[$item['value']] . ' (' . $item['value'] . ')';
  23. break;
  24. case 'label':
  25. $diff_items[$delta] = $allowed_values[$item['value']];
  26. break;
  27. default:
  28. $diff_items[$delta] = $item['value'];
  29. break;
  30. }
  31. }
  32. else {
  33. // If no match was found for the label, fall back to the key.
  34. $diff_items[$delta] = $item['value'];
  35. }
  36. }
  37. return $diff_items;
  38. }
  39. /**
  40. * Provide default field comparison options.
  41. */
  42. function list_field_diff_default_options($field_type) {
  43. return array(
  44. 'compare' => 'label',
  45. );
  46. }
  47. /**
  48. * Provide a form for setting the field comparison options.
  49. */
  50. function list_field_diff_options_form($field_type, $settings) {
  51. $options_form = array();
  52. $options_form['compare'] = array(
  53. '#type' => 'radios',
  54. '#title' => t('Comparison method'),
  55. '#options' => array(
  56. 'label' => t('Label'),
  57. 'key' => t('Key'),
  58. 'both' => t('Label (key)'),
  59. ),
  60. '#default_value' => $settings['compare'],
  61. );
  62. return $options_form;
  63. }