tripal_fields.module 14 KB

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