tripal_fields.fields.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * Implements hook_field_info().
  4. */
  5. function tripal_fields_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_fields_organism_select_widget',
  11. 'default_formatter' => 'tripal_fields_organism_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_fields',
  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_fields_primary_dbxref_widget',
  23. 'default_formatter' => 'tripal_fields_primary_dbxref_formatter',
  24. 'settings' => array(),
  25. 'storage' => array(
  26. 'type' => 'field_chado_storage',
  27. 'module' => 'tripal_fields',
  28. 'active' => TRUE
  29. ),
  30. ),
  31. 'residues' => array(
  32. 'label' => t('Residues'),
  33. 'description' => t('A field for managing nucleotide and protein residues.'),
  34. 'default_widget' => 'tripal_fields_residue_textarea_widget',
  35. 'default_formatter' => 'tripal_fields_residues_formatter',
  36. 'settings' => array(),
  37. 'storage' => array(
  38. 'type' => 'field_chado_storage',
  39. 'module' => 'tripal_fields',
  40. 'active' => TRUE
  41. ),
  42. ),
  43. 'md5checksum' => array(
  44. 'label' => t('MD5 checksum'),
  45. 'description' => t('A field for generating MD5 checksum for a sequence.'),
  46. 'default_widget' => 'tripal_fields_md5checksum_checkbox_widget',
  47. 'default_formatter' => 'tripal_fields_md5checksum_formatter',
  48. 'settings' => array(),
  49. 'storage' => array(
  50. 'type' => 'field_chado_storage',
  51. 'module' => 'tripal_fields',
  52. 'active' => TRUE
  53. ),
  54. ),
  55. 'seqlen' => array(
  56. 'label' => t('Sequence length'),
  57. 'description' => t('A field for calculating the length of a sequence.'),
  58. 'default_widget' => 'tripal_fields_seqlen_hidden_widget',
  59. 'default_formatter' => 'tripal_fields_seqlen_formatter',
  60. 'settings' => array(),
  61. 'storage' => array(
  62. 'type' => 'field_chado_storage',
  63. 'module' => 'tripal_fields',
  64. 'active' => TRUE
  65. ),
  66. ),
  67. // The field provides a widget for adding new properties
  68. // to an entity that is connected to a base table that has a prop table
  69. // in Chado.
  70. 'kvproperty' => array(
  71. 'label' => t('Add a Property Type'),
  72. 'description' => t('This record may have any number of properties. Use this field to first add the type.'),
  73. 'default_widget' => 'tripal_fields_kvproperty_widget',
  74. 'default_formatter' => 'tripal_fields_kvproperty_formatter',
  75. 'settings' => array(),
  76. 'storage' => array(
  77. 'type' => 'field_chado_storage',
  78. 'module' => 'tripal_fields',
  79. 'active' => TRUE
  80. ),
  81. ),
  82. );
  83. return $fields;
  84. }
  85. /**
  86. * Implements hook_field_widget_info().
  87. */
  88. function tripal_fields_field_widget_info() {
  89. return array(
  90. 'tripal_fields_organism_select_widget' => array(
  91. 'label' => t('Organism Select'),
  92. 'field types' => array('organism_id')
  93. ),
  94. 'tripal_fields_primary_dbxref_widget' => array(
  95. 'label' => t('Cross-reference'),
  96. 'field types' => array('dbxref_id'),
  97. '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.'),
  98. ),
  99. 'tripal_fields_md5checksum_checkbox_widget' => array(
  100. 'label' => t('MD5 Checksum Checkbox'),
  101. 'field types' => array('md5checksum'),
  102. ),
  103. 'tripal_fields_residues_textarea_widget' => array(
  104. 'label' => t('Residues'),
  105. 'field types' => array('residues'),
  106. ),
  107. 'tripal_fields_seqlen_hidden_widget' => array(
  108. 'label' => t('Sequence Length'),
  109. 'field types' => array('seqlen'),
  110. ),
  111. 'tripal_fields_kvproperty_widget' => array(
  112. 'label' => t('Property'),
  113. 'field types' => array('kvproperty'),
  114. ),
  115. );
  116. }
  117. /**
  118. * Implements hook_field_formatter_info().
  119. */
  120. function tripal_fields_field_formatter_info() {
  121. return array(
  122. 'tripal_fields_organism_formatter' => array(
  123. 'label' => t('Organism'),
  124. 'field types' => array('organism_id')
  125. ),
  126. 'tripal_fields_primary_dbxref_formatter' => array(
  127. 'label' => t('Cross-reference'),
  128. 'field types' => array('dbxref_id')
  129. ),
  130. 'tripal_fields_md5checksum_formatter' => array(
  131. 'label' => t('MD5 checksum'),
  132. 'field types' => array('md5checksum')
  133. ),
  134. 'tripal_fields_residues_formatter' => array(
  135. 'label' => t('Residues'),
  136. 'field types' => array('residues')
  137. ),
  138. 'tripal_fields_seqlen_formatter' => array(
  139. 'label' => t('Sequence length'),
  140. 'field types' => array('seqlen')
  141. ),
  142. 'tripal_fields_kvproperty_formatter' => array(
  143. 'label' => t('Property'),
  144. 'field types' => array('kvproperty')
  145. ),
  146. );
  147. }
  148. /**
  149. * Implements hook_field_formatter_view().
  150. *
  151. * Two formatters are implemented.
  152. * - field_example_simple_text just outputs markup indicating the color that
  153. * was entered and uses an inline style to set the text color to that value.
  154. * - field_example_color_background does the same but also changes the
  155. * background color of div.region-content.
  156. *
  157. * @see field_example_field_formatter_info()
  158. */
  159. function tripal_fields_field_formatter_view($entity_type, $entity, $field,
  160. $instance, $langcode, $items, $display) {
  161. $element = array();
  162. switch ($display['type']) {
  163. case 'tripal_fields_organism_formatter':
  164. module_load_include('inc', 'tripal_fields', 'includes/fields/organism_id');
  165. tripal_fields_organism_select_formatter($element, $entity_type, $entity, $field,
  166. $instance, $langcode, $items, $display);
  167. break;
  168. case 'tripal_fields_primary_dbxref_formatter':
  169. module_load_include('inc', 'tripal_fields', 'includes/fields/primary_dbxref');
  170. tripal_fields_primary_dbxref_formatter($element, $entity_type, $entity, $field,
  171. $instance, $langcode, $items, $display);
  172. break;
  173. case 'tripal_fields_md5checksum_formatter':
  174. module_load_include('inc', 'tripal_fields', 'includes/fields/md5checksum');
  175. tripal_fields_md5checksum_checkbox_formatter($element, $entity_type, $entity, $field,
  176. $instance, $langcode, $items, $display);
  177. break;
  178. case 'tripal_fields_residues_formatter':
  179. module_load_include('inc', 'tripal_fields', 'includes/fields/residues');
  180. tripal_fields_residues_textarea_formatter($element, $entity_type, $entity, $field,
  181. $instance, $langcode, $items, $display);
  182. break;
  183. case 'tripal_fields_seqlen_formatter':
  184. module_load_include('inc', 'tripal_fields', 'includes/fields/seqlen');
  185. tripal_fields_seqlen_hidden_formatter($element, $entity_type, $entity, $field,
  186. $instance, $langcode, $items, $display);
  187. break;
  188. case 'tripal_fields_kvproperty_formatter':
  189. module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty');
  190. tripal_fields_kvproperty_formatter($element, $entity_type, $entity, $field,
  191. $instance, $langcode, $items, $display);
  192. break;
  193. }
  194. return $element;
  195. }
  196. /**
  197. * Implements hook_field_widget_form().
  198. */
  199. function tripal_fields_field_widget_form(&$form, &$form_state, $field,
  200. $instance, $langcode, $items, $delta, $element) {
  201. $widget = $element;
  202. $widget['#delta'] = $delta;
  203. $field_name = $field['field_name'];
  204. switch ($instance['widget']['type']) {
  205. case 'tripal_fields_organism_select_widget':
  206. // Make sure the include files get parsed now and for the form submits.
  207. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/organism_id');
  208. module_load_include('inc', 'tripal_fields', 'includes/fields/organism_id');
  209. // Update the widget with the new field.
  210. tripal_fields_organism_select_widget($field_name, $widget, $form,
  211. $form_state, $field, $instance, $langcode, $items, $delta, $element);
  212. $element['value'] = $widget;
  213. break;
  214. case 'tripal_fields_primary_dbxref_widget':
  215. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/primary_dbxref');
  216. module_load_include('inc', 'tripal_fields', 'includes/fields/primary_dbxref');
  217. tripal_fields_primary_dbxref_widget($field_name, $widget, $form,
  218. $form_state, $field, $instance, $langcode, $items, $delta, $element);
  219. $element['value'] = $widget;
  220. break;
  221. case 'tripal_fields_md5checksum_checkbox_widget':
  222. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/md5checksum');
  223. module_load_include('inc', 'tripal_fields', 'includes/fields/md5checksum');
  224. tripal_fields_md5checksum_checkbox_widget($field_name, $widget,
  225. $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  226. $element['value'] = $widget;
  227. break;
  228. case 'tripal_fields_residues_textarea_widget':
  229. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/residues');
  230. module_load_include('inc', 'tripal_fields', 'includes/fields/residues');
  231. tripal_fields_residues_textarea_widget($field_name, $widget,
  232. $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  233. $element['value'] = $widget;
  234. break;
  235. case 'tripal_fields_seqlen_hidden_widget':
  236. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/seqlen');
  237. module_load_include('inc', 'tripal_fields', 'includes/fields/seqlen');
  238. tripal_fields_seqlen_hidden_widget($field_name, $widget,
  239. $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  240. $element['value'] = $widget;
  241. break;
  242. case 'tripal_fields_kvproperty_widget':
  243. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/kvproperty');
  244. module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty');
  245. tripal_fields_kvproperty_widget($field_name, $widget,
  246. $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  247. $element['value'] = $widget;
  248. break;
  249. }
  250. return $element;
  251. }
  252. /**
  253. * Implements hook_field_is_empty().
  254. */
  255. function tripal_fields_field_is_empty($item, $field) {
  256. if (empty($item['value']) && (string) $item['value'] !== '0') {
  257. return TRUE;
  258. }
  259. return FALSE;
  260. }
  261. /**
  262. * Returns the values of the field from the $form_state.
  263. */
  264. function tripal_fields_get_field_form_values($field_name, $form_state, $child = NULL) {
  265. $values = array();
  266. if (!array_key_exists('values', $form_state)) {
  267. return $values;
  268. }
  269. if (array_key_exists($field_name, $form_state['values'])) {
  270. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  271. foreach ($items as $delta => $value) {
  272. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  273. $values[] = $value['value'][0][$child];
  274. }
  275. else if (!$child and $value['value']) {
  276. $values[] = $value['value'];
  277. }
  278. }
  279. }
  280. }
  281. return $values;
  282. }
  283. /**
  284. * Returns the values of the field from the $form_state.
  285. */
  286. function tripal_fields_set_field_form_values($field_name, &$form_state, $newvalue, $child = NULL) {
  287. $values = array();
  288. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  289. foreach ($items as $delta => $value) {
  290. if ($child and array_key_exists($child, $value['value'][0]) and $value['value'][0][$child]) {
  291. $form_state['values'][$field_name][$langcode][$delta]['value'][0][$child] = $newvalue;
  292. }
  293. else if (!$child) {
  294. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  295. }
  296. }
  297. }
  298. return $values;
  299. }