chado_base__organism_id.inc 9.0 KB

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