chado_feature__residues.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. class chado_feature__residues extends TripalField {
  3. /**
  4. * Implements hook_info() for fields.
  5. *
  6. * This is a hook provided by the tripal_chado module for offloading the
  7. * hook_field_info() hook for each field to specify.
  8. */
  9. function field_info() {
  10. return array(
  11. 'label' => t('Residues'),
  12. 'description' => t('A field for managing nucleotide and protein residues.'),
  13. 'default_widget' => 'chado_feature__residues_widget',
  14. 'default_formatter' => 'chado_feature__residues_formatter',
  15. 'settings' => array(),
  16. 'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
  17. 'storage' => array(
  18. 'type' => 'field_chado_storage',
  19. 'module' => 'tripal_chado',
  20. 'active' => TRUE
  21. ),
  22. );
  23. }
  24. /**
  25. * Implements hook_attach_info().
  26. *
  27. * This is a hook provided by the tripal_Chado module. It allows the field
  28. * to specify which bundles it will attach to and to specify thee settings.
  29. *
  30. * @param $entity_type
  31. * @param $entity
  32. * @param $term
  33. *
  34. * @return
  35. * A field array
  36. */
  37. function attach_info($entity_type, $bundle, $settings) {
  38. $field_info = array();
  39. $table_name = $settings['data_table'];
  40. $type_table = $settings['type_table'];
  41. $type_field = $settings['field'];
  42. $cv_id = $settings['cv_id'];
  43. $cvterm_id = $settings['cvterm_id'];
  44. // If this is not the feature table then we don't want to attach.
  45. if ($table_name != 'feature') {
  46. return $field_info;
  47. }
  48. $field_info = array(
  49. 'field_name' => 'feature__residues',
  50. 'field_type' => 'chado_feature__residues',
  51. 'widget_type' => 'chado_feature__residues_widget',
  52. 'description' => 'An IUPAC compatible residues for this feature.',
  53. 'label' => 'Residues',
  54. 'is_required' => 0,
  55. 'storage' => 'field_chado_storage',
  56. 'widget_settings' => array(
  57. 'display_label' => 1
  58. ),
  59. 'field_settings' => array(
  60. 'chado_table' => $table_name,
  61. 'chado_column' => 'residues',
  62. 'semantic_web' => array(
  63. 'name' => '',
  64. 'accession' => '',
  65. 'ns' => '',
  66. 'nsurl' => '',
  67. ),
  68. ),
  69. );
  70. return $field_info;
  71. }
  72. /**
  73. * Implements hook_widget_info.
  74. *
  75. * This is a hook provided by the tripal_chado module for offloading
  76. * the hook_field_widget_info() hook for each field to specify.
  77. */
  78. function widget_info() {
  79. return array(
  80. 'label' => t('Residues'),
  81. 'field types' => array('chado_feature__residues'),
  82. );
  83. }
  84. /**
  85. * Implements hook_formatter_info.
  86. *
  87. * This is a hook provided by the tripal_chado module for
  88. * offloading the hook_field_formatter_info() for each field
  89. * to specify.
  90. *
  91. */
  92. function formatter_info() {
  93. return array(
  94. 'label' => t('Residues'),
  95. 'field types' => array('chado_feature__residues'),
  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 formatter_view(&$element, $entity_type, $entity, $field,
  109. $instance, $langcode, $items, $display) {
  110. foreach ($items as $delta => $item) {
  111. $residues = key_exists('value', $item) ? $item['value'] : '';
  112. $content = '<pre class="residues-formatter">' . $residues . '</pre>';
  113. $element[$delta] = array(
  114. // We create a render array to produce the desired markup,
  115. '#type' => 'markup',
  116. '#markup' => $content,
  117. );
  118. }
  119. }
  120. /**
  121. *
  122. * @param unknown $field_name
  123. * @param unknown $widget
  124. * @param unknown $form
  125. * @param unknown $form_state
  126. * @param unknown $field
  127. * @param unknown $instance
  128. * @param unknown $langcode
  129. * @param unknown $items
  130. * @param unknown $delta
  131. * @param unknown $element
  132. */
  133. function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  134. $widget['value'] = array(
  135. '#type' => 'textarea',
  136. '#title' => $element['#title'],
  137. '#description' => $element['#description'],
  138. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  139. '#default_value' => count($items) > 0 ? $items[0]['value'] : '',
  140. '#delta' => $delta,
  141. '#element_validate' => array('chado_feature__residues_widget_validate'),
  142. '#cols' => 30,
  143. );
  144. }
  145. }
  146. /**
  147. * Callback function for validating the chado_feature__residues_widget.
  148. */
  149. function chado_feature__residues_widget_validate($element, &$form_state) {
  150. $field_name = $element['#parents'][0];
  151. // Remove any white spaces.
  152. $residues = tripal_chado_get_field_form_values($field_name, $form_state);
  153. if ($residues) {
  154. $residues = preg_replace('/\s/', '', $residues);
  155. tripal_chado_set_field_form_values($field_name, $form_state, $residues);
  156. }
  157. }