tripal_fields.module 14 KB

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