tripal_entities.fields.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Implements hook_field_info().
  4. */
  5. function tripal_entities_field_info() {
  6. $fields = array(
  7. 'organism_id' => array(
  8. 'label' => t('Organism'),
  9. 'description' => t('A field for specifying an organism.'),
  10. 'default_widget' => 'tripal_entities_organism_select_widget',
  11. 'default_formatter' => 'tripal_entities_organism_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'tripal_entities_storage',
  15. 'module' => 'tripal_entities',
  16. 'active' => TRUE
  17. ),
  18. ),
  19. );
  20. return $fields;
  21. }
  22. /**
  23. * Implements hook_field_widget_info().
  24. */
  25. function tripal_entities_field_widget_info() {
  26. return array(
  27. 'tripal_entities_organism_select_widget' => array(
  28. 'label' => t('Organism Select'),
  29. 'field types' => array('organism_id')
  30. ),
  31. );
  32. }
  33. /**
  34. * Implements hook_field_formatter_info().
  35. */
  36. function tripal_entities_field_formatter_info() {
  37. return array(
  38. 'tripal_entities_organism_formatter' => array(
  39. 'label' => t('Organism'),
  40. 'field types' => array('organism_id')
  41. ),
  42. );
  43. }
  44. /**
  45. * Implements hook_field_formatter_view().
  46. *
  47. * Two formatters are implemented.
  48. * - field_example_simple_text just outputs markup indicating the color that
  49. * was entered and uses an inline style to set the text color to that value.
  50. * - field_example_color_background does the same but also changes the
  51. * background color of div.region-content.
  52. *
  53. * @see field_example_field_formatter_info()
  54. */
  55. function tripal_entities_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  56. $element = array();
  57. switch ($display['type']) {
  58. // This formatter simply outputs the field as text and with a color.
  59. case 'tripal_entities_organism_formatter':
  60. foreach ($items as $delta => $item) {
  61. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
  62. $element[$delta] = array(
  63. // We create a render array to produce the desired markup,
  64. // "<p>Genus Species</p>".
  65. // See theme_html_tag().
  66. '#type' => 'html_tag',
  67. '#tag' => 'p',
  68. '#value' => '<i>' . $organism[0]->genus .' ' . $organism[0]->species . '</i>',
  69. );
  70. }
  71. break;
  72. }
  73. return $element;
  74. }
  75. /**
  76. * Implements hook_field_widget_form().
  77. */
  78. function tripal_entities_field_widget_form(&$form, &$form_state, $field,
  79. $instance, $langcode, $items, $delta, $element) {
  80. $widget = $element;
  81. $widget['#delta'] = $delta;
  82. switch ($instance['widget']['type']) {
  83. case 'tripal_entities_organism_select_widget':
  84. $options = tripal_get_organism_select_options();
  85. $widget += array(
  86. '#type' => 'select',
  87. '#title' => $element['#title'],
  88. '#description' => $element['#description'],
  89. '#options' => $options,
  90. '#default_value' => $items[0]['value'],
  91. '#required' => $element['#required'],
  92. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  93. '#delta' => $delta,
  94. '#element_validate' => array('tripal_entities_organism_select_widget_validate'),
  95. );
  96. $element['value'] = $widget;
  97. break;
  98. }
  99. return $element;
  100. }
  101. /**
  102. * Implements hook_field_is_empty().
  103. */
  104. function tripal_entities_field_is_empty($item, $field) {
  105. if (empty($item['value']) && (string) $item['value'] !== '0') {
  106. return TRUE;
  107. }
  108. return FALSE;
  109. }
  110. /**
  111. * Callback function for validating the tripal_entities_organism_select_widget.
  112. */
  113. function tripal_entities_organism_select_widget_validate($element, &$form_state) {
  114. $field_name = $element['#field_name'];
  115. // Make sure we have a valid organism
  116. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  117. foreach ($items as $delta => $value) {
  118. $organism_id = chado_select_record('organism', array('organism_id'),
  119. array('organism_id' => $value['value']), array('has_record' => TRUE));
  120. if (!$organism_id) {
  121. form_error($element, t("Please specify an organism that already exists in the database."));
  122. }
  123. }
  124. }
  125. }