tripal_fields.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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_chado_field_alter().
  178. *
  179. * This function is used to change the default field formatter and widget
  180. * that are assigned to fields of an Entity. This hook is only used for
  181. * those fields that correspond to a column in a Chado table. An implementation
  182. * of this hook can be used to change the default formatters and widgets to
  183. * custom formatters and widgets that are created by the module creating
  184. * this hook.
  185. *
  186. * By default, Tripal will provide custom formatters and widgets for many
  187. * columns in Chado tables, therefore, this hook will most likely be of use
  188. * to extension modules that create custom table inside of Chado.
  189. *
  190. * @param $field
  191. */
  192. function hook_chado_field_alter(&$field) {
  193. // TODO: add example code for how to use this hook.
  194. }
  195. /**
  196. * Implements hook_chado_field_alter().
  197. *
  198. * This function adds the custom formatters and widgets to many of the Chado
  199. * tables. This way Tripal users get a nice set of already usable fields.
  200. */
  201. function tripal_fields_chado_field_alter(&$field) {
  202. if (!array_key_exists('field_settings', $field)) {
  203. return;
  204. }
  205. // If the field doesn't list the Chado table or column then just return.
  206. if (!array_key_exists('chado_table', $field['field_settings']) or
  207. !array_key_exists('chado_column', $field['field_settings'])) {
  208. return;
  209. }
  210. // Here we provide new field types and widgets for FK fields
  211. // and fields that need special attention.
  212. if ($field['field_settings']['chado_column'] =='organism_id') {
  213. $field['field_type'] = 'organism_id';
  214. $field['widget_type'] = 'tripal_fields_organism_select_widget';
  215. $field['label'] = 'Organism';
  216. $field['description'] = 'Select an organism.';
  217. }
  218. else if ($field['field_settings']['chado_column'] =='dbxref_id') {
  219. $field['field_type'] = 'dbxref_id';
  220. $field['widget_type'] = 'tripal_fields_primary_dbxref_widget';
  221. $field['label'] = 'Primary Cross Reference';;
  222. $field['description'] = 'This record can be cross-referenced with a
  223. record in another online database. The primary reference is for the
  224. most prominent reference. At a minimum, the database and accession
  225. must be provided. To remove a set reference, change the database
  226. field to "Select a Database".';
  227. }
  228. else if ($field['field_settings']['chado_table'] == 'feature' and
  229. $field['field_settings']['chado_column'] == 'md5checksum') {
  230. $field['field_type'] = 'md5checksum';
  231. $field['widget_type'] = 'tripal_fields_md5checksum_checkbox_widget';
  232. $field['label'] = 'MD5 Checksum';
  233. $field['description'] = 'Generating MD5 checksum for the sequence.';
  234. }
  235. else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
  236. $field['field_type'] = 'seqlen';
  237. $field['widget_type'] = 'tripal_fields_seqlen_hidden_widget';
  238. $field['label'] = 'Seqlen';
  239. $field['description'] = 'The length of the residues.';
  240. }
  241. else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
  242. $field['field_type'] = 'residues';
  243. $field['widget_type'] = 'tripal_fields_residues_textarea_widget';
  244. $field['label'] = 'Residues';
  245. $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
  246. }
  247. else if ($field['label'] == 'Timeaccessioned') {
  248. $field['label'] = 'Time Accessioned';
  249. $field['description'] = 'Please enter the time that this record was first added to the database.';
  250. }
  251. else if ($field['label'] == 'Timelastmodified') {
  252. $field['label'] = 'Time Last Modified';
  253. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  254. }
  255. }
  256. /**
  257. * Implements hook_field_widget_form_alter().
  258. */
  259. function tripal_fields_field_widget_form_alter(&$element, &$form_state, $context) {
  260. if (array_key_exists('#field_name', $element)) {
  261. $field_name = $element['#field_name'];
  262. $matches = array();
  263. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  264. $tablename = $matches[1];
  265. $colname = $matches[2];
  266. $schema = chado_get_schema($tablename);
  267. // The timelastmodified field exists in many Chado tables. We want
  268. // the form element to update to the most recent time rather than the time
  269. // in the database.
  270. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  271. // We want the default value for the field to be the current time.
  272. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  273. $element['#date_items']['value'] = $element['#default_value']['value'];
  274. }
  275. // We want the date combo fieldset to be collaspible so we will
  276. // add our own theme_wrapper to replace the one added by the date
  277. // module.
  278. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  279. $element['#theme_wrappers'] = array('tripal_entities_date_combo');
  280. }
  281. }
  282. }
  283. }
  284. /**
  285. * Implements hook_field_formatter_view().
  286. */
  287. function tripal_fields_field_formatter_view($entity_type, $entity, $field,
  288. $instance, $langcode, $items, $display) {
  289. $element = array();
  290. switch ($display['type']) {
  291. case 'tripal_fields_organism_formatter':
  292. module_load_include('inc', 'tripal_fields', 'includes/fields/organism_id');
  293. tripal_fields_organism_select_formatter($element, $entity_type, $entity, $field,
  294. $instance, $langcode, $items, $display);
  295. break;
  296. case 'tripal_fields_dbxref_id_formatter':
  297. module_load_include('inc', 'tripal_fields', 'includes/fields/dbxref_id');
  298. tripal_fields_dbxref_id_formatter($element, $entity_type, $entity, $field,
  299. $instance, $langcode, $items, $display);
  300. break;
  301. case 'tripal_fields_md5checksum_formatter':
  302. module_load_include('inc', 'tripal_fields', 'includes/fields/md5checksum');
  303. tripal_fields_md5checksum_checkbox_formatter($element, $entity_type, $entity, $field,
  304. $instance, $langcode, $items, $display);
  305. break;
  306. case 'tripal_fields_residues_formatter':
  307. module_load_include('inc', 'tripal_fields', 'includes/fields/residues');
  308. tripal_fields_residues_textarea_formatter($element, $entity_type, $entity, $field,
  309. $instance, $langcode, $items, $display);
  310. break;
  311. case 'tripal_fields_seqlen_formatter':
  312. module_load_include('inc', 'tripal_fields', 'includes/fields/seqlen');
  313. tripal_fields_seqlen_hidden_formatter($element, $entity_type, $entity, $field,
  314. $instance, $langcode, $items, $display);
  315. break;
  316. case 'tripal_fields_kvproperty_adder_formatter':
  317. module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty_adder');
  318. tripal_fields_kvproperty_adder_formatter($element, $entity_type, $entity, $field,
  319. $instance, $langcode, $items, $display);
  320. break;
  321. case 'tripal_fields_kvproperty_formatter':
  322. module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty');
  323. tripal_fields_kvproperty_formatter($element, $entity_type, $entity, $field,
  324. $instance, $langcode, $items, $display);
  325. break;
  326. }
  327. return $element;
  328. }
  329. /**
  330. * Implements hook_field_widget_form().
  331. */
  332. function tripal_fields_field_widget_form(&$form, &$form_state, $field,
  333. $instance, $langcode, $items, $delta, $element) {
  334. $widget = $element;
  335. switch ($instance['widget']['type']) {
  336. case 'tripal_fields_organism_select_widget':
  337. // Make sure the include files get parsed now and for the form submits.
  338. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/organism_id');
  339. module_load_include('inc', 'tripal_fields', 'includes/fields/organism_id');
  340. // Update the widget with the new field.
  341. tripal_fields_organism_select_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  342. break;
  343. case 'tripal_fields_dbxref_id_widget':
  344. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/dbxref_id');
  345. module_load_include('inc', 'tripal_fields', 'includes/fields/dbxref_id');
  346. tripal_fields_dbxref_id_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  347. break;
  348. case 'tripal_fields_md5checksum_checkbox_widget':
  349. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/md5checksum');
  350. module_load_include('inc', 'tripal_fields', 'includes/fields/md5checksum');
  351. tripal_fields_md5checksum_checkbox_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  352. break;
  353. case 'tripal_fields_residues_textarea_widget':
  354. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/residues');
  355. module_load_include('inc', 'tripal_fields', 'includes/fields/residues');
  356. tripal_fields_residues_textarea_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  357. break;
  358. case 'tripal_fields_seqlen_hidden_widget':
  359. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/seqlen');
  360. module_load_include('inc', 'tripal_fields', 'includes/fields/seqlen');
  361. tripal_fields_seqlen_hidden_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  362. break;
  363. case 'tripal_fields_kvproperty_adder_widget':
  364. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/kvproperty_adder');
  365. module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty_adder');
  366. tripal_fields_kvproperty_adder_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  367. break;
  368. case 'tripal_fields_kvproperty_widget':
  369. form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/kvproperty');
  370. module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty');
  371. tripal_fields_kvproperty_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  372. break;
  373. }
  374. return $widget;
  375. }
  376. /**
  377. * Implements hook_form_FORM_ID_alter().
  378. *
  379. * The field_ui_display_overview_form is used for formatting the display
  380. * or layout of fields attached to an entity and shown on the entity view page.
  381. */
  382. function tripal_fields_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  383. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  384. // on the add/edit form of an entity for adding new property fields.
  385. $fields_names = element_children($form['fields']);
  386. foreach ($fields_names as $field_name) {
  387. $field_info = field_info_field($field_name);
  388. if ($field_info['type'] == 'kvproperty_adder') {
  389. unset($form['fields'][$field_name]);
  390. }
  391. }
  392. }
  393. /**
  394. * Implements hook_form_FORM_ID_alter().
  395. *
  396. * The field_ui_field_overview_form is used for ordering and configuring the
  397. * fields attached to an entity.
  398. */
  399. function tripal_fields_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  400. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  401. // on the add/edit form of an entity for adding new property fields.
  402. $fields_names = element_children($form['fields']);
  403. foreach ($fields_names as $field_name) {
  404. $field_info = field_info_field($field_name);
  405. if ($field_info['type'] == 'kvproperty_adder') {
  406. unset($form['fields'][$field_name]);
  407. }
  408. }
  409. }
  410. /**
  411. * Implements hook_field_is_empty().
  412. */
  413. function tripal_fields_field_is_empty($item, $field) {
  414. // If there is no value field then the field is empty.
  415. if (!array_key_exists('value', $item)) {
  416. return TRUE;
  417. }
  418. // Iterate through all of the fields and if at least one has a value
  419. // the field is not empty.
  420. foreach ($item as $form_field_name => $value) {
  421. if (isset($value) and $value != NULL and $value != '') {
  422. return FALSE;
  423. }
  424. }
  425. // Otherwise, the field is empty.
  426. return TRUE;
  427. }
  428. /**
  429. * Returns the values of the field from the $form_state.
  430. */
  431. function tripal_fields_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
  432. $value = NULL;
  433. // The form_state must have the 'values' key. If not then just return.
  434. if (!array_key_exists('values', $form_state)) {
  435. return $value;
  436. }
  437. // If the field name is not in the form_state['values'] then return.
  438. if (!array_key_exists($field_name, $form_state['values'])) {
  439. return $value;
  440. }
  441. // Iterate through the values looking for the field_name provided.
  442. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  443. $item = $items[$delta];
  444. if ($child){
  445. if(array_key_exists($child, $item) and $item[$child] != '') {
  446. $value = $item[$child];
  447. }
  448. }
  449. else {
  450. $value = $item['value'];
  451. }
  452. }
  453. return $value;
  454. }
  455. /**
  456. * Sets the values of the field from the $form_state.
  457. */
  458. function tripal_fields_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
  459. // The form_state must have the 'values' key. If not then just return.
  460. if (!array_key_exists('values', $form_state)) {
  461. return FALSE;
  462. }
  463. // If the field name is not in the form_state['values'] then reutrn.
  464. if (!array_key_exists($field_name, $form_state['values'])) {
  465. return FALSE;
  466. }
  467. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  468. if ($child) {
  469. $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
  470. }
  471. else {
  472. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  473. }
  474. }
  475. return TRUE;
  476. }
  477. /**
  478. * Implements hook_theme().
  479. */
  480. function tripal_fields_theme($existing, $type, $theme, $path) {
  481. return array(
  482. 'tripal_fields_dbxref_id_widget' => array(
  483. 'render element' => 'element',
  484. 'file' => 'includes/fields/dbxref_id.inc',
  485. ),
  486. 'tripal_fields_kvproperty_addr_widget' => array(
  487. 'render element' => 'element',
  488. 'file' => 'includes/fields/dbxref_id.inc',
  489. ),
  490. );
  491. }