tripal_entities.fields.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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' => 'field_chado_storage',
  15. 'module' => 'tripal_entities',
  16. 'active' => TRUE
  17. ),
  18. ),
  19. 'dbxref_id' => array(
  20. 'label' => t('Primary Cross-reference'),
  21. 'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference. At a minimum, the database and accession must be provided.'),
  22. 'default_widget' => 'tripal_entities_primary_dbxref_widget',
  23. 'default_formatter' => 'tripal_entities_primary_dbxref_formatter',
  24. 'settings' => array(),
  25. 'storage' => array(
  26. 'type' => 'field_chado_storage',
  27. 'module' => 'tripal_entities',
  28. 'active' => TRUE
  29. ),
  30. ),
  31. );
  32. return $fields;
  33. }
  34. /**
  35. * Implements hook_field_widget_info().
  36. */
  37. function tripal_entities_field_widget_info() {
  38. return array(
  39. 'tripal_entities_organism_select_widget' => array(
  40. 'label' => t('Organism Select'),
  41. 'field types' => array('organism_id')
  42. ),
  43. 'tripal_entities_primary_dbxref_widget' => array(
  44. 'label' => t('Primary Cross-reference'),
  45. 'field types' => array('dbxref_id'),
  46. 'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference. At a minimum, the database and accession must be provided.'),
  47. ),
  48. );
  49. }
  50. /**
  51. * Implements hook_field_formatter_info().
  52. */
  53. function tripal_entities_field_formatter_info() {
  54. return array(
  55. 'tripal_entities_organism_formatter' => array(
  56. 'label' => t('Organism'),
  57. 'field types' => array('organism_id')
  58. ),
  59. 'tripal_entities_primary_dbxref_formatter' => array(
  60. 'label' => t('Primary Cross-reference'),
  61. 'field types' => array('dbxref_id')
  62. ),
  63. );
  64. }
  65. /**
  66. * Implements hook_field_formatter_view().
  67. *
  68. * Two formatters are implemented.
  69. * - field_example_simple_text just outputs markup indicating the color that
  70. * was entered and uses an inline style to set the text color to that value.
  71. * - field_example_color_background does the same but also changes the
  72. * background color of div.region-content.
  73. *
  74. * @see field_example_field_formatter_info()
  75. */
  76. function tripal_entities_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  77. $element = array();
  78. switch ($display['type']) {
  79. case 'tripal_entities_organism_formatter':
  80. foreach ($items as $delta => $item) {
  81. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
  82. $element[$delta] = array(
  83. // We create a render array to produce the desired markup,
  84. // "<p>Genus Species</p>".
  85. // See theme_html_tag().
  86. '#type' => 'markup',
  87. '#markup' => '<i>' . $organism[0]->genus .' ' . $organism[0]->species . '</i>',
  88. );
  89. }
  90. break;
  91. case 'tripal_entities_primary_dbxref_formatter':
  92. foreach ($items as $delta => $item) {
  93. $accession = '';
  94. if ($item['value']) {
  95. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $item['value']));
  96. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  97. if ($dbxref->db_id->urlprefix) {
  98. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  99. }
  100. }
  101. $element[$delta] = array(
  102. // We create a render array to produce the desired markup,
  103. '#type' => 'markup',
  104. '#markup' => $accession,
  105. );
  106. }
  107. break;
  108. }
  109. return $element;
  110. }
  111. /**
  112. * Implements hook_field_widget_form().
  113. */
  114. function tripal_entities_field_widget_form(&$form, &$form_state, $field,
  115. $instance, $langcode, $items, $delta, $element) {
  116. $widget = $element;
  117. $widget['#delta'] = $delta;
  118. switch ($instance['widget']['type']) {
  119. case 'tripal_entities_organism_select_widget':
  120. $options = tripal_get_organism_select_options();
  121. $widget += array(
  122. '#type' => 'select',
  123. '#title' => $element['#title'],
  124. '#description' => $element['#description'],
  125. '#options' => $options,
  126. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  127. '#required' => $element['#required'],
  128. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  129. '#delta' => $delta,
  130. '#element_validate' => array('tripal_entities_organism_select_widget_validate'),
  131. );
  132. $element['value'] = $widget;
  133. break;
  134. case 'tripal_entities_primary_dbxref_widget':
  135. // Get the field defaults from the database if a record exists.
  136. $dbxref_id = '';
  137. $db_id = '';
  138. $accession = '';
  139. $version = '';
  140. $description = '';
  141. if (count($items) > 0 and $items[0]['value']) {
  142. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
  143. $dbxref_id = $dbxref->dbxref_id;
  144. $db_id = $dbxref->db_id->db_id;
  145. $accession = $dbxref->accession;
  146. $version = $dbxref->version;
  147. $description = $dbxref->description;
  148. }
  149. $schema = chado_get_schema('dbxref');
  150. $options = tripal_get_db_select_options();
  151. $widget += array(
  152. '#element_validate' => array('tripal_entities_primary_dbxref_widget_validate'),
  153. '#type' => 'fieldset',
  154. '#title' => $element['#title'],
  155. '#description' => 'Hi', // $element['#description'],
  156. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  157. '#delta' => $delta,
  158. '#theme' => 'tripal_entities_primary_dbxref_widget',
  159. array(
  160. $element['#field_name'] => array(
  161. '#type' => 'hidden',
  162. '#default_value' => $dbxref_id,
  163. ),
  164. 'dbxref__db_id' => array(
  165. '#type' => 'select',
  166. '#title' => t('Database'),
  167. '#options' => $options,
  168. '#required' => $element['#required'],
  169. '#default_value' => $db_id,
  170. ),
  171. 'dbxref__accession' => array(
  172. '#type' => 'textfield',
  173. '#title' => t('Accession'),
  174. '#default_value' => $accession,
  175. '#required' => $element['#required'],
  176. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  177. '#size' => 15,
  178. ),
  179. 'dbxref__version' => array(
  180. '#type' => 'textfield',
  181. '#title' => t('Version'),
  182. '#default_value' => $version,
  183. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  184. '#size' => 5,
  185. ),
  186. 'dbxref__description' => array(
  187. '#type' => 'textfield',
  188. '#title' => t('Description'),
  189. '#default_value' => $description,
  190. '#size' => 20,
  191. ),
  192. ),
  193. );
  194. $element['value'] = $widget;
  195. break;
  196. }
  197. return $element;
  198. }
  199. /**
  200. * Implements hook_field_is_empty().
  201. */
  202. function tripal_entities_field_is_empty($item, $field) {
  203. if (empty($item['value']) && (string) $item['value'] !== '0') {
  204. return TRUE;
  205. }
  206. return FALSE;
  207. }
  208. /**
  209. * Callback function for validating the tripal_entities_organism_select_widget.
  210. */
  211. function tripal_entities_organism_select_widget_validate($element, &$form_state) {
  212. $field_name = $element['#field_name'];
  213. $organism_id = tripal_entities_get_field_form_values($field_name, $form_state);
  214. if (count($organism_id) == 0) {
  215. form_error($element, t("Please specify an organism that already exists in the database."));
  216. }
  217. }
  218. /**
  219. * Callback function for validating the tripal_entities_organism_select_widget.
  220. */
  221. function tripal_entities_primary_dbxref_widget_validate($element, &$form_state) {
  222. $field_name = $element['#field_name'];
  223. // Get the field values.
  224. $db_id = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__db_id");
  225. $accession = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__accession");
  226. $version = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__version");
  227. $description = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__description");
  228. // Make sure that if a database ID is provided that an accession is also
  229. // provided. Here we use the form_set_error function rather than the
  230. // form_error function because the form_error will add a red_highlight
  231. // around all of the fields in the fieldset which is confusing as it's not
  232. // clear to the user what field is required and which isn't. Therefore,
  233. // we borrow the code from the 'form_error' function and append the field
  234. // so that the proper field is highlighted on error.
  235. if (count($db_id) == 0 and count($accession) > 0) {
  236. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__db_id', t("A database and the accession must both be provided for the primary cross reference."));
  237. }
  238. if (count($db_id) > 0 and count($accession) == 0) {
  239. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  240. }
  241. }
  242. /**
  243. * Theme function for the primary_dbxref_widget.
  244. *
  245. * @param $variables
  246. */
  247. function theme_tripal_entities_primary_dbxref_widget($variables) {
  248. $element = $variables['element'];
  249. $layout = "
  250. <div class=\"primary-dbxref-widget\">
  251. <div class=\"primary-dbxref-widget-item\">" .
  252. drupal_render($element[0]['dbxref__db_id']) . "
  253. </div>
  254. <div class=\"primary-dbxref-widget-item\">" .
  255. drupal_render($element[0]['dbxref__accession']) . "
  256. </div>
  257. <div class=\"primary-dbxref-widget-item\">" .
  258. drupal_render($element[0]['dbxref__version']) . "
  259. </div>
  260. <div class=\"primary-dbxref-widget-item\">" .
  261. drupal_render($element[0]['dbxref__description']) . "
  262. </div>
  263. </div>
  264. ";
  265. return $layout;
  266. }
  267. /**
  268. * Returns the values of the field from the $form_state.
  269. */
  270. function tripal_entities_get_field_form_values($field_name, $form_state, $child = NULL) {
  271. $values = array();
  272. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  273. foreach ($items as $delta => $value) {
  274. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  275. $values[] = $value['value'][0][$child];
  276. }
  277. else if (!$child and $value['value']) {
  278. $values[] = $value['value'];
  279. }
  280. }
  281. }
  282. return $values;
  283. }