chado_base__organism_id.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. *
  4. */
  5. function chado_base__organism_id_formatter(&$element, $entity_type, $entity, $field,
  6. $instance, $langcode, $items, $display) {
  7. foreach ($items as $delta => $item) {
  8. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
  9. $content = '<i>' . $organism[0]->genus .' ' . $organism[0]->species . '</i>';
  10. $element[$delta] = array(
  11. '#type' => 'markup',
  12. '#markup' => $content,
  13. );
  14. }
  15. }
  16. /**
  17. * Loads the field values with appropriate data.
  18. *
  19. * This function is called by the tripal_chado_field_storage_load() for
  20. * each property managed by the field_chado_storage storage type. This is
  21. * an optional hook function that is only needed if the field has
  22. * multiple form elements.
  23. *
  24. * @param $field
  25. * @param $entity
  26. * @param $base_table
  27. * @param $record
  28. */
  29. function chado_base__organism_id_load($field, $entity, $base_table, $record) {
  30. $field_name = $field['field_name'];
  31. // The organism_id is already set as the value. We need to get it and
  32. // see if there is a published entity for this organism. If there is
  33. // then we want to add the organism object generated using chado_generate_var
  34. // as well as the entity to the record.
  35. $organism_id = $entity->{$field_name}['und'][0]['value'];
  36. $organism = chado_generate_var('organism', array('organism_id' => $organism_id));
  37. $entity->{$field_name}['und'][0]['organism'] = $organism;
  38. $org_entity = tripal_load_chado_entity('organism', $organism_id);
  39. $entity->{$field_name}['und'][0]['entity'] = $org_entity;
  40. }
  41. /**
  42. * Implements hook_ws_formatter().
  43. */
  44. function chado_base__organism_id_ws_formatter(&$element, $entity_type, $entity,
  45. $field, $instance, $items) {
  46. foreach ($items as $delta => $item) {
  47. $organism = $item['organism'];
  48. $entity = $item['entity'];
  49. if ($entity) {
  50. $element[$delta]['#entity'] = $entity;
  51. }
  52. $element[$delta]['genus'] = $organism->genus;
  53. $element[$delta]['species'] = $organism->species;
  54. $element[$delta]['common_name'] = $organism->common_name;
  55. }
  56. }
  57. /**
  58. * Implements hook_widget().
  59. */
  60. function chado_base__organism_id_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  61. $options = tripal_get_organism_select_options(FALSE);
  62. $widget['value'] = array(
  63. '#type' => 'select',
  64. '#title' => $element['#title'],
  65. '#description' => $element['#description'],
  66. '#options' => $options,
  67. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  68. '#required' => $element['#required'],
  69. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  70. '#delta' => $delta,
  71. '#element_validate' => array('chado_base__organism_id_widget_validate'),
  72. );
  73. $widget['add_organism'] = array(
  74. '#type' => 'item',
  75. '#markup' => l('Add a new species', 'admin/content/bio_data/add/species', array('attributes' => array('target' => '_blank'))),
  76. );
  77. }
  78. /**
  79. * Callback function for validating the chado_base__organism_id_widget.
  80. */
  81. function chado_base__organism_id_widget_validate($element, &$form_state) {
  82. $field_name = $element['#parents'][0];
  83. // If the form ID is field_ui_field_edit_form, then the user is editing the
  84. // field's values in the manage fields form of Drupal. We don't want
  85. // to validate it as if it were being used in a data entry form.
  86. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  87. return;
  88. }
  89. $organism_id = tripal_chado_get_field_form_values($field_name, $form_state);
  90. if (!$organism_id) {
  91. form_error($element, t("Please specify an organism."));
  92. }
  93. }
  94. /**
  95. * Provides a settings form for the formatter.
  96. *
  97. * This function is equiavlent to the hook_field_formatter_settings_form()
  98. * hook.
  99. *
  100. * @param $field
  101. * The field structure being configured.
  102. * @param $instance
  103. * The instance structure being configured.
  104. * @param $view_mode
  105. * The view mode being configured.
  106. * @param $form
  107. * The (entire) configuration form array, which will usually have no use here.
  108. * @param $form_state
  109. * The form state of the (entire) configuration form.
  110. */
  111. function chado_base__organism_id_formatter_form($field, $instance,
  112. $view_mode, $form, &$form_state) {
  113. $display = $instance['display'][$view_mode];
  114. $settings = $display['settings'];
  115. $element = array();
  116. $term = NULL;
  117. $bundle = NULL;
  118. // Check to see if the organism bundle exists
  119. $term = tripal_load_term_entity(array(
  120. 'namespace' => $field['settings']['semantic_web']['ns'],
  121. 'accession' => $field['settings']['semantic_web']['type']
  122. ));
  123. if ($term) {
  124. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  125. }
  126. $element['instructions'] = array(
  127. '#type' => 'item',
  128. '#markup' => 'Please provide the format for viewing the organism. You
  129. can specify the format using tokens that correspond to each field'
  130. );
  131. $element['field_display'] = array(
  132. '#type' => 'textfield',
  133. '#title' => 'Display Format',
  134. '#description' => t('Provide a mixture of text and/or tokens for the format.
  135. For example: [organism__genus] [organism__species]. When displayed,
  136. the tokens will be replaced with the actual value.'),
  137. '#default_value' => '[organism__genus] [organism__species]',
  138. );
  139. $element['field_display_entity'] = array(
  140. '#type' => 'checkbox',
  141. '#title' => 'Display teaser if available',
  142. '#description' => t('If the organism that this field is associated with is
  143. a published page then display the teaser rather use the display
  144. format above.'),
  145. );
  146. $element['tokens'] = array(
  147. '#type' => 'fieldset',
  148. '#collapsed' => TRUE,
  149. '#collapsible' => TRUE,
  150. '#title' => 'Available Tokens'
  151. );
  152. $headers = array('Token', 'Description');
  153. $rows = array();
  154. $tokens = tripal_get_tokens($bundle);
  155. foreach ($tokens as $token) {
  156. $rows[] = array(
  157. $token['token'],
  158. $token['description'],
  159. );
  160. }
  161. $table_vars = array(
  162. 'header' => $headers,
  163. 'rows' => $rows,
  164. 'attributes' => array(),
  165. 'sticky' => FALSE,
  166. 'caption' => '',
  167. 'colgroups' => array(),
  168. 'empty' => 'There are no tokens',
  169. );
  170. $project_details = theme('table', $table_vars);
  171. $element['tokens']['list'] = array(
  172. '#type' => 'item',
  173. '#markup' => theme_table($table_vars),
  174. );
  175. return $element;
  176. }