tripal_entities.fields.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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('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('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('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. // TODO: add hook here to allow other modules to change this display
  83. // if they want.
  84. $element[$delta] = array(
  85. // We create a render array to produce the desired markup,
  86. // "<p>Genus Species</p>".
  87. // See theme_html_tag().
  88. '#type' => 'markup',
  89. '#markup' => '<i>' . $organism[0]->genus .' ' . $organism[0]->species . '</i>',
  90. );
  91. }
  92. break;
  93. case 'tripal_entities_primary_dbxref_formatter':
  94. foreach ($items as $delta => $item) {
  95. $accession = '';
  96. if ($item['value']) {
  97. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $item['value']));
  98. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  99. if ($dbxref->db_id->urlprefix) {
  100. $accession = l($accession, $dbxref->db_id->urlprefix . '/' . $dbxref->accession);
  101. }
  102. }
  103. // TODO: add hook here to allow other modules to change this display
  104. // if they want.
  105. $element[$delta] = array(
  106. // We create a render array to produce the desired markup,
  107. '#type' => 'markup',
  108. '#markup' => $accession,
  109. );
  110. }
  111. break;
  112. }
  113. return $element;
  114. }
  115. /**
  116. * Implements hook_field_widget_form().
  117. */
  118. function tripal_entities_field_widget_form(&$form, &$form_state, $field,
  119. $instance, $langcode, $items, $delta, $element) {
  120. $widget = $element;
  121. $widget['#delta'] = $delta;
  122. switch ($instance['widget']['type']) {
  123. case 'tripal_entities_organism_select_widget':
  124. $options = tripal_get_organism_select_options();
  125. $widget += array(
  126. '#type' => 'select',
  127. '#title' => $element['#title'],
  128. '#description' => $element['#description'],
  129. '#options' => $options,
  130. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  131. '#required' => $element['#required'],
  132. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  133. '#delta' => $delta,
  134. '#element_validate' => array('tripal_entities_organism_select_widget_validate'),
  135. );
  136. $element['value'] = $widget;
  137. break;
  138. case 'tripal_entities_primary_dbxref_widget':
  139. // Get the field defaults from the database if a record exists.
  140. $dbxref_id = '';
  141. $db_id = '';
  142. $accession = '';
  143. $version = '';
  144. $description = '';
  145. if (count($items) > 0 and $items[0]['value']) {
  146. $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
  147. $dbxref_id = $dbxref->dbxref_id;
  148. $db_id = $dbxref->db_id->db_id;
  149. $accession = $dbxref->accession;
  150. $version = $dbxref->version;
  151. $description = $dbxref->description;
  152. }
  153. $schema = chado_get_schema('dbxref');
  154. $options = tripal_get_db_select_options();
  155. $widget += array(
  156. '#element_validate' => array('tripal_entities_primary_dbxref_widget_validate'),
  157. '#type' => 'fieldset',
  158. '#title' => $element['#title'],
  159. '#description' => $element['#description'],
  160. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  161. '#delta' => $delta,
  162. '#theme' => 'tripal_entities_primary_dbxref_widget',
  163. array(
  164. $element['#field_name'] => array(
  165. '#type' => 'hidden',
  166. '#default_value' => $dbxref_id,
  167. ),
  168. 'dbxref__db_id' => array(
  169. '#type' => 'select',
  170. '#title' => t('Database'),
  171. '#options' => $options,
  172. '#required' => $element['#required'],
  173. '#default_value' => $db_id,
  174. ),
  175. 'dbxref__accession' => array(
  176. '#type' => 'textfield',
  177. '#title' => t('Accession'),
  178. '#default_value' => $accession,
  179. '#required' => $element['#required'],
  180. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  181. '#size' => 15,
  182. ),
  183. 'dbxref__version' => array(
  184. '#type' => 'textfield',
  185. '#title' => t('Version'),
  186. '#default_value' => $version,
  187. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  188. '#size' => 5,
  189. ),
  190. 'dbxref__description' => array(
  191. '#type' => 'textfield',
  192. '#title' => t('Description'),
  193. '#default_value' => $description,
  194. '#size' => 20,
  195. ),
  196. ),
  197. );
  198. $element['value'] = $widget;
  199. break;
  200. }
  201. return $element;
  202. }
  203. /**
  204. * Implements hook_field_is_empty().
  205. */
  206. function tripal_entities_field_is_empty($item, $field) {
  207. if (empty($item['value']) && (string) $item['value'] !== '0') {
  208. return TRUE;
  209. }
  210. return FALSE;
  211. }
  212. /**
  213. * Callback function for validating the tripal_entities_organism_select_widget.
  214. */
  215. function tripal_entities_organism_select_widget_validate($element, &$form_state) {
  216. $field_name = $element['#field_name'];
  217. $organism_id = tripal_entities_get_field_form_values($field_name, $form_state);
  218. if (count($organism_id) == 0) {
  219. form_error($element, t("Please specify an organism that already exists in the database."));
  220. }
  221. }
  222. /**
  223. * Callback function for validating the tripal_entities_organism_select_widget.
  224. */
  225. function tripal_entities_primary_dbxref_widget_validate($element, &$form_state) {
  226. $field_name = $element['#field_name'];
  227. // Get the field values.
  228. $db_id = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__db_id");
  229. $accession = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__accession");
  230. $version = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__version");
  231. $description = tripal_entities_get_field_form_values($field_name, $form_state, "dbxref__description");
  232. // Make sure that if a database ID is provided that an accession is also
  233. // provided. Here we use the form_set_error function rather than the
  234. // form_error function because the form_error will add a red_highlight
  235. // around all of the fields in the fieldset which is confusing as it's not
  236. // clear to the user what field is required and which isn't. Therefore,
  237. // we borrow the code from the 'form_error' function and append the field
  238. // so that the proper field is highlighted on error.
  239. if (count($db_id) == 0 and count($accession) > 0) {
  240. 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."));
  241. }
  242. if (count($db_id) > 0 and count($accession) == 0) {
  243. form_set_error(implode('][', $element ['#parents']) . '][0][dbxref__accession', t("A database and the accession must both be provided for the primary cross reference."));
  244. }
  245. }
  246. /**
  247. * Theme function for the primary_dbxref_widget.
  248. *
  249. * @param $variables
  250. */
  251. function theme_tripal_entities_primary_dbxref_widget($variables) {
  252. $element = $variables['element'];
  253. $layout = "
  254. <div class=\"primary-dbxref-widget\">
  255. <div class=\"primary-dbxref-widget-item\">" .
  256. drupal_render($element[0]['dbxref__db_id']) . "
  257. </div>
  258. <div class=\"primary-dbxref-widget-item\">" .
  259. drupal_render($element[0]['dbxref__accession']) . "
  260. </div>
  261. <div class=\"primary-dbxref-widget-item\">" .
  262. drupal_render($element[0]['dbxref__version']) . "
  263. </div>
  264. <div class=\"primary-dbxref-widget-item\">" .
  265. drupal_render($element[0]['dbxref__description']) . "
  266. </div>
  267. </div>
  268. ";
  269. return $layout;
  270. }
  271. /**
  272. * Returns the values of the field from the $form_state.
  273. */
  274. function tripal_entities_get_field_form_values($field_name, $form_state, $child = NULL) {
  275. $values = array();
  276. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  277. foreach ($items as $delta => $value) {
  278. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  279. $values[] = $value['value'][0][$child];
  280. }
  281. else if (!$child and $value['value']) {
  282. $values[] = $value['value'];
  283. }
  284. }
  285. }
  286. return $values;
  287. }