chado_feature__seqlen.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. 'type' => '',
  62. 'ns' => '',
  63. 'nsurl' => '',
  64. ),
  65. ),
  66. );
  67. return $field_info;
  68. }
  69. /**
  70. * Implements hook_widget_info.
  71. *
  72. * This is a hook provided by the tripal_chado module for offloading
  73. * the hook_field_widget_info() hook for each field to specify.
  74. */
  75. function chado_feature__seqlen_widget_info() {
  76. return array(
  77. 'label' => t('Sequence Length'),
  78. 'field types' => array('chado_feature__seqlen'),
  79. );
  80. }
  81. /**
  82. * Implements hook_formatter_info.
  83. *
  84. * This is a hook provided by the tripal_chado module for
  85. * offloading the hook_field_formatter_info() for each field
  86. * to specify.
  87. *
  88. */
  89. function chado_feature__seqlen_formatter_info() {
  90. return array(
  91. 'label' => t('Residues Length'),
  92. 'field types' => array('chado_feature__seqlen'),
  93. 'settings' => array(
  94. ),
  95. );
  96. }
  97. /**
  98. *
  99. * @param unknown $entity_type
  100. * @param unknown $entity
  101. * @param unknown $field
  102. * @param unknown $instance
  103. * @param unknown $langcode
  104. * @param unknown $items
  105. * @param unknown $display
  106. */
  107. function chado_feature__seqlen_formatter(&$element, $entity_type, $entity, $field,
  108. $instance, $langcode, $items, $display) {
  109. foreach ($items as $delta => $item) {
  110. $content = key_exists('value', $item) ? $item['value'] : '';
  111. $element[$delta] = array(
  112. // We create a render array to produce the desired markup,
  113. '#type' => 'markup',
  114. '#markup' => $content,
  115. );
  116. }
  117. }
  118. /**
  119. *
  120. * @param unknown $field_name
  121. * @param unknown $widget
  122. * @param unknown $form
  123. * @param unknown $form_state
  124. * @param unknown $field
  125. * @param unknown $instance
  126. * @param unknown $langcode
  127. * @param unknown $items
  128. * @param unknown $delta
  129. * @param unknown $element
  130. */
  131. function chado_feature__seqlen_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  132. $widget['value'] = array(
  133. '#type' => 'hidden',
  134. '#title' => $element['#title'],
  135. '#description' => $element['#description'],
  136. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  137. '#delta' => $delta,
  138. '#element_validate' => array('chado_feature__seqlen_widget_validate'),
  139. );
  140. }
  141. /**
  142. * Callback function for validating the chado_feature__seqlen_widget.
  143. */
  144. function chado_feature__seqlen_widget_validate($element, &$form_state) {
  145. $field_name = $element['#parents'][0];
  146. // Get the residues so we can calculate teh length.
  147. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state);
  148. // Remove any white spaces.
  149. if ($residues) {
  150. $residues = preg_replace('/\s/', '', $residues);
  151. tripal_chado_set_field_form_values($field_name, $form_state, strlen($residues));
  152. }
  153. else {
  154. // Otherwise, remove the md5 value
  155. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
  156. }
  157. }