chado_feature__seqlen.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. */
  8. function chado_feature__seqlen_info() {
  9. return array(
  10. 'label' => t('Sequence length'),
  11. 'description' => t('A field for calculating the length of a sequence.'),
  12. 'default_widget' => 'chado_feature__seqlen_widget',
  13. 'default_formatter' => 'chado_feature__seqlen_formatter',
  14. 'settings' => array(),
  15. 'storage' => array(
  16. 'type' => 'field_chado_storage',
  17. 'module' => 'tripal_chado',
  18. 'active' => TRUE
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements hook_attach_info().
  24. *
  25. * This is a hook provided by the tripal_Chado module. It allows the field
  26. * to specify which bundles it will attach to and to specify thee settings.
  27. *
  28. * @param $entity_type
  29. * @param $entity
  30. * @param $term
  31. *
  32. * @return
  33. * A field array
  34. */
  35. function chado_feature__seqlen_attach_info($entity_type, $bundle, $target) {
  36. $field_info = array();
  37. $table_name = $target['data_table'];
  38. $type_table = $target['type_table'];
  39. $type_field = $target['field'];
  40. $cv_id = $target['cv_id'];
  41. $cvterm_id = $target['cvterm_id'];
  42. // If this is not the feature table then we don't want to attach.
  43. if ($table_name != 'feature') {
  44. return $field_info;
  45. }
  46. $field_info = array(
  47. 'field_name' => 'feature__seqlen',
  48. 'field_type' => 'chado_feature__seqlen',
  49. 'widget_type' => 'chado_feature__seqlen_widget',
  50. 'description' => 'The length of the sequence (residues).',
  51. 'label' => 'Sequence Length',
  52. 'is_required' => 0,
  53. 'storage' => 'field_chado_storage',
  54. 'widget_settings' => array(
  55. 'display_label' => 1
  56. ),
  57. 'field_settings' => array(
  58. 'chado_table' => $table_name,
  59. 'chado_column' => 'seqlen',
  60. 'semantic_web' => array(
  61. 'name' => '',
  62. 'accession' => '',
  63. 'ns' => '',
  64. 'nsurl' => '',
  65. ),
  66. ),
  67. );
  68. return $field_info;
  69. }
  70. /**
  71. * Implements hook_widget_info.
  72. *
  73. * This is a hook provided by the tripal_chado module for offloading
  74. * the hook_field_widget_info() hook for each field to specify.
  75. */
  76. function chado_feature__seqlen_widget_info() {
  77. return array(
  78. 'label' => t('Sequence Length'),
  79. 'field types' => array('chado_feature__seqlen'),
  80. );
  81. }
  82. /**
  83. * Implements hook_formatter_info.
  84. *
  85. * This is a hook provided by the tripal_chado module for
  86. * offloading the hook_field_formatter_info() for each field
  87. * to specify.
  88. *
  89. */
  90. function chado_feature__seqlen_formatter_info() {
  91. return array(
  92. 'label' => t('Residues Length'),
  93. 'field types' => array('chado_feature__seqlen'),
  94. 'settings' => array(
  95. ),
  96. );
  97. }
  98. /**
  99. *
  100. * @param unknown $entity_type
  101. * @param unknown $entity
  102. * @param unknown $field
  103. * @param unknown $instance
  104. * @param unknown $langcode
  105. * @param unknown $items
  106. * @param unknown $display
  107. */
  108. function chado_feature__seqlen_formatter(&$element, $entity_type, $entity, $field,
  109. $instance, $langcode, $items, $display) {
  110. foreach ($items as $delta => $item) {
  111. $content = key_exists('value', $item) ? $item['value'] : '';
  112. $element[$delta] = array(
  113. // We create a render array to produce the desired markup,
  114. '#type' => 'markup',
  115. '#markup' => $content,
  116. );
  117. }
  118. }
  119. /**
  120. *
  121. * @param unknown $field_name
  122. * @param unknown $widget
  123. * @param unknown $form
  124. * @param unknown $form_state
  125. * @param unknown $field
  126. * @param unknown $instance
  127. * @param unknown $langcode
  128. * @param unknown $items
  129. * @param unknown $delta
  130. * @param unknown $element
  131. */
  132. function chado_feature__seqlen_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  133. $widget['value'] = array(
  134. '#type' => 'hidden',
  135. '#title' => $element['#title'],
  136. '#description' => $element['#description'],
  137. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  138. '#delta' => $delta,
  139. '#element_validate' => array('chado_feature__seqlen_widget_validate'),
  140. );
  141. }
  142. /**
  143. * Callback function for validating the chado_feature__seqlen_widget.
  144. */
  145. function chado_feature__seqlen_widget_validate($element, &$form_state) {
  146. $field_name = $element['#parents'][0];
  147. // Get the residues so we can calculate teh length.
  148. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state);
  149. // Remove any white spaces.
  150. if ($residues) {
  151. $residues = preg_replace('/\s/', '', $residues);
  152. tripal_chado_set_field_form_values($field_name, $form_state, strlen($residues));
  153. }
  154. else {
  155. // Otherwise, remove the md5 value
  156. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
  157. }
  158. }