chado_feature__residues.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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__residues_info() {
  9. return array(
  10. 'label' => t('Residues'),
  11. 'description' => t('A field for managing nucleotide and protein residues.'),
  12. 'default_widget' => 'chado_feature__residues_widget',
  13. 'default_formatter' => 'chado_feature__residues_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__residues_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__residues',
  48. 'field_type' => 'chado_feature__residues',
  49. 'widget_type' => 'chado_feature__residues_widget',
  50. 'description' => 'An IUPAC compatible residues for this feature.',
  51. 'label' => 'Residues',
  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' => 'residues',
  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__residues_widget_info() {
  76. return array(
  77. 'label' => t('Residues'),
  78. 'field types' => array('chado_feature__residues'),
  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__residues_formatter_info() {
  90. return array(
  91. 'label' => t('Residues'),
  92. 'field types' => array('chado_feature__residues'),
  93. );
  94. }
  95. /**
  96. *
  97. * @param unknown $entity_type
  98. * @param unknown $entity
  99. * @param unknown $field
  100. * @param unknown $instance
  101. * @param unknown $langcode
  102. * @param unknown $items
  103. * @param unknown $display
  104. */
  105. function chado_feature__residues_formatter(&$element, $entity_type, $entity, $field,
  106. $instance, $langcode, $items, $display) {
  107. foreach ($items as $delta => $item) {
  108. $residues = key_exists('value', $item) ? $item['value'] : '';
  109. $content = '<pre class="residues-formatter">' . $residues . '</pre>';
  110. $element[$delta] = array(
  111. // We create a render array to produce the desired markup,
  112. '#type' => 'markup',
  113. '#markup' => $content,
  114. );
  115. }
  116. }
  117. /**
  118. *
  119. * @param unknown $field_name
  120. * @param unknown $widget
  121. * @param unknown $form
  122. * @param unknown $form_state
  123. * @param unknown $field
  124. * @param unknown $instance
  125. * @param unknown $langcode
  126. * @param unknown $items
  127. * @param unknown $delta
  128. * @param unknown $element
  129. */
  130. function chado_feature__residues_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  131. $widget['value'] = array(
  132. '#type' => 'textarea',
  133. '#title' => $element['#title'],
  134. '#description' => $element['#description'],
  135. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  136. '#default_value' => count($items) > 0 ? $items[0]['value'] : '',
  137. '#delta' => $delta,
  138. '#element_validate' => array('chado_feature__residues_widget_validate'),
  139. '#cols' => 30,
  140. );
  141. }
  142. /**
  143. * Callback function for validating the chado_feature__residues_widget.
  144. */
  145. function chado_feature__residues_widget_validate($element, &$form_state) {
  146. $field_name = $element['#parents'][0];
  147. // Remove any white spaces.
  148. $residues = tripal_chado_get_field_form_values($field_name, $form_state);
  149. if ($residues) {
  150. $residues = preg_replace('/\s/', '', $residues);
  151. tripal_chado_set_field_form_values($field_name, $form_state, $residues);
  152. }
  153. }