chado_base__organism_id.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_attach_info().
  24. *
  25. * This is a hook provided by the tripal_Chado module. It allows the field
  26. * to specify which bundles it will attach to and to specify thee settings.
  27. *
  28. * @param $entity_type
  29. * @param $entity
  30. * @param $term
  31. *
  32. * @return
  33. * A field array
  34. */
  35. function chado_base__organism_id_attach_info($entity_type, $bundle, $target) {
  36. $field_info = array();
  37. $table_name = $target['data_table'];
  38. $type_table = $target['type_table'];
  39. $type_field = $target['field'];
  40. $cv_id = $target['cv_id'];
  41. $cvterm_id = $target['cvterm_id'];
  42. // Check the schema for the data table if it does not have
  43. // an 'organism_id' column then we don't want to attach this field.
  44. $schema = chado_get_schema($table_name);
  45. if (!array_key_exists('organism_id', $schema['fields'])) {
  46. return $field_info;
  47. }
  48. // There is an organism_id column so attach the field!
  49. $field_info = array(
  50. 'field_name' => $table_name . '__organism_id',
  51. 'field_type' => 'chado_base__organism_id',
  52. 'widget_type' => 'chado_base__organism_id_widget',
  53. 'description' => 'Select an organism.',
  54. 'label' => 'Oraganism',
  55. 'is_required' => 0,
  56. 'storage' => 'field_chado_storage',
  57. 'widget_settings' => array(
  58. 'display_label' => 1
  59. ),
  60. 'field_settings' => array(
  61. 'chado_table' => $table_name,
  62. 'chado_column' => 'organism_id',
  63. 'semantic_web' => array(
  64. 'type' => 'organism',
  65. 'ns' => 'local',
  66. 'nsurl' => '',
  67. ),
  68. ),
  69. );
  70. return $field_info;
  71. }
  72. /**
  73. * Implements hook_widget_info.
  74. *
  75. * This is a hook provided by the tripal_chado module for offloading
  76. * the hook_field_widget_info() hook for each field to specify.
  77. */
  78. function chado_base__organism_id_widget_info() {
  79. return array(
  80. 'label' => t('Organism Select'),
  81. 'field types' => array('chado_base__organism_id')
  82. );
  83. }
  84. /**
  85. * Implements hook_formatter_info.
  86. *
  87. * This is a hook provided by the tripal_chado module for
  88. * offloading the hook_field_formatter_info() for each field
  89. * to specify.
  90. *
  91. */
  92. function chado_base__organism_id_formatter_info() {
  93. return array(
  94. 'label' => t('Organism'),
  95. 'field types' => array('chado_base__organism_id'),
  96. 'settings' => array(
  97. 'field_display_teaser' => 0,
  98. 'field_display_string' => '<i>[organism__genus] [organism__species]</i>',
  99. ),
  100. );
  101. }
  102. /**
  103. * Implements hook_formatter_settings_summary.
  104. *
  105. * This is a hook provided by the tripal_chado module for
  106. * offloading the hook_field_formatter_settings_summary() for each field
  107. * to specify.
  108. *
  109. */
  110. function chado_base__organism_id_formatter_settings_summary($field, $instance, $view_mode) {
  111. $display = $instance['display'][$view_mode];
  112. $settings = $display['settings'];
  113. $summary = 'Settings summary: ';
  114. if (array_key_exists('field_display_teaser', $settings) and
  115. $settings['field_display_teaser'] == 1) {
  116. $summary .= 'displays the organism teaser if published.';
  117. }
  118. else if (array_key_exists('field_display_string', $settings)) {
  119. $summary .= 'uses the token string "' . $settings['field_display_string'] . '"';
  120. }
  121. else {
  122. $summary .= "uses display defaults.";
  123. }
  124. return $summary;
  125. }
  126. /**
  127. * Provides a settings form for the formatter.
  128. *
  129. * This is a hook provided by the tripal_chado module for
  130. * offloading the hook_field_formatter_settings_form() for each field
  131. * to specify.
  132. */
  133. function chado_base__organism_id_formatter_settings_form($field, $instance,
  134. $view_mode, $form, &$form_state) {
  135. $display = $instance['display'][$view_mode];
  136. $settings = $display['settings'];
  137. $element = array();
  138. $term = NULL;
  139. $entity = NULL;
  140. // Check to see if the organism bundle exists
  141. $term = tripal_load_term_entity(array(
  142. 'namespace' => $field['settings']['semantic_web']['ns'],
  143. 'accession' => $field['settings']['semantic_web']['type']
  144. ));
  145. if ($term) {
  146. $entity = tripal_load_bundle_entity(array('term_id' => $term->id));
  147. }
  148. $element['instructions'] = array(
  149. '#type' => 'item',
  150. '#markup' => 'Please provide the format for viewing the organism. You
  151. can specify the format using tokens that correspond to each field'
  152. );
  153. $element['field_display_string'] = array(
  154. '#type' => 'textfield',
  155. '#title' => 'Display Format',
  156. '#description' => t('Provide a mixture of text and/or tokens for the format.
  157. For example: [organism__genus] [organism__species]. When displayed,
  158. the tokens will be replaced with the actual value.'),
  159. '#default_value' => $settings['field_display_string'],
  160. );
  161. $element['field_display_teaser'] = array(
  162. '#type' => 'checkbox',
  163. '#title' => 'Display teaser if available',
  164. '#description' => t('If the organism that this field is associated with is
  165. a published page then display the teaser rather use the display
  166. format above.'),
  167. '#default_value' => $settings['field_display_teaser'],
  168. );
  169. $element['tokens'] = array(
  170. '#type' => 'fieldset',
  171. '#collapsed' => TRUE,
  172. '#collapsible' => TRUE,
  173. '#title' => 'Available Tokens'
  174. );
  175. $headers = array('Token', 'Description');
  176. $rows = array();
  177. // Here we use the tripal_get_chado_tokens rather than the
  178. // tripal_get_entity_tokens because we can't gurantee that all organisms
  179. // have entities.
  180. $tokens = tripal_get_chado_tokens('organism');
  181. foreach ($tokens as $token) {
  182. $rows[] = array(
  183. $token['token'],
  184. $token['description'],
  185. );
  186. }
  187. $table_vars = array(
  188. 'header' => $headers,
  189. 'rows' => $rows,
  190. 'attributes' => array(),
  191. 'sticky' => FALSE,
  192. 'caption' => '',
  193. 'colgroups' => array(),
  194. 'empty' => 'There are no tokens',
  195. );
  196. $project_details = theme('table', $table_vars);
  197. $element['tokens']['list'] = array(
  198. '#type' => 'item',
  199. '#markup' => theme_table($table_vars),
  200. );
  201. $element['#element_validate'] = array('chado_base__organism_id_formatter_settings_form_validate');
  202. return $element;
  203. }
  204. /**
  205. * Validation function for the chado_base__organism_id_formatter_settings_form.
  206. */
  207. function chado_base__organism_id_formatter_settings_form_validate(&$form, &$form_state) {
  208. // Place here as an example for validating the settings form.
  209. }
  210. /**
  211. *
  212. */
  213. function chado_base__organism_id_formatter(&$element, $entity_type, $entity,
  214. $field, $instance, $langcode, $items, $display) {
  215. // Get the settings
  216. $settings = $display['settings'];
  217. $record = $entity->chado_record;
  218. foreach ($items as $delta => $item) {
  219. $organism = $record->organism_id;
  220. if ($settings['field_display_teaser']) {
  221. }
  222. else {
  223. $field_name = $field['field_name'];
  224. $string = $settings['field_display_string'];
  225. $field_data = $entity->$field_name;
  226. $content = tripal_replace_chado_tokens($string, $organism);
  227. $element[$delta] = array(
  228. '#type' => 'markup',
  229. '#markup' => $content,
  230. );
  231. }
  232. }
  233. }
  234. /**
  235. * Implements hook_ws_formatter().
  236. */
  237. function chado_base__organism_id_ws_formatter($entity_type, $entity,
  238. $field, $instance, $items) {
  239. $values = array();
  240. foreach ($items as $delta => $item) {
  241. }
  242. return $values;
  243. }
  244. /**
  245. * Implements hook_widget().
  246. */
  247. function chado_base__organism_id_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  248. $options = tripal_get_organism_select_options(FALSE);
  249. $widget['value'] = array(
  250. '#type' => 'select',
  251. '#title' => $element['#title'],
  252. '#description' => $element['#description'],
  253. '#options' => $options,
  254. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  255. '#required' => $element['#required'],
  256. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  257. '#delta' => $delta,
  258. '#element_validate' => array('chado_base__organism_id_widget_validate'),
  259. );
  260. $widget['add_organism'] = array(
  261. '#type' => 'item',
  262. '#markup' => l('Add a new species', 'admin/content/bio_data/add/species', array('attributes' => array('target' => '_blank'))),
  263. );
  264. }
  265. /**
  266. * Callback function for validating the chado_base__organism_id_widget.
  267. */
  268. function chado_base__organism_id_widget_validate($element, &$form_state) {
  269. $field_name = $element['#parents'][0];
  270. // If the form ID is field_ui_field_edit_form, then the user is editing the
  271. // field's values in the manage fields form of Drupal. We don't want
  272. // to validate it as if it were being used in a data entry form.
  273. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  274. return;
  275. }
  276. $organism_id = tripal_chado_get_field_form_values($field_name, $form_state);
  277. if (!$organism_id) {
  278. form_error($element, t("Please specify an organism."));
  279. }
  280. }