chado_feature__residues.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. '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__residues_widget_info() {
  77. return array(
  78. 'label' => t('Residues'),
  79. 'field types' => array('chado_feature__residues'),
  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__residues_formatter_info() {
  91. return array(
  92. 'label' => t('Residues'),
  93. 'field types' => array('chado_feature__residues'),
  94. );
  95. }
  96. /**
  97. *
  98. * @param unknown $entity_type
  99. * @param unknown $entity
  100. * @param unknown $field
  101. * @param unknown $instance
  102. * @param unknown $langcode
  103. * @param unknown $items
  104. * @param unknown $display
  105. */
  106. function chado_feature__residues_formatter(&$element, $entity_type, $entity, $field,
  107. $instance, $langcode, $items, $display) {
  108. foreach ($items as $delta => $item) {
  109. $residues = key_exists('value', $item) ? $item['value'] : '';
  110. $content = '<pre class="residues-formatter">' . $residues . '</pre>';
  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__residues_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  132. $widget['value'] = array(
  133. '#type' => 'textarea',
  134. '#title' => $element['#title'],
  135. '#description' => $element['#description'],
  136. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  137. '#default_value' => count($items) > 0 ? $items[0]['value'] : '',
  138. '#delta' => $delta,
  139. '#element_validate' => array('chado_feature__residues_widget_validate'),
  140. '#cols' => 30,
  141. );
  142. }
  143. /**
  144. * Callback function for validating the chado_feature__residues_widget.
  145. */
  146. function chado_feature__residues_widget_validate($element, &$form_state) {
  147. $field_name = $element['#parents'][0];
  148. // Remove any white spaces.
  149. $residues = tripal_chado_get_field_form_values($field_name, $form_state);
  150. if ($residues) {
  151. $residues = preg_replace('/\s/', '', $residues);
  152. tripal_chado_set_field_form_values($field_name, $form_state, $residues);
  153. }
  154. }