chado_base__organism_id.inc 8.1 KB

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