chado_feature__residues.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_widget_info.
  24. *
  25. * This is a hook provided by the tripal_chado module for offloading
  26. * the hook_field_widget_info() hook for each field to specify.
  27. */
  28. function chado_feature__residues_widget_info() {
  29. return array(
  30. 'label' => t('Residues'),
  31. 'field types' => array('chado_feature__residues'),
  32. );
  33. }
  34. /**
  35. * Implements hook_formatter_info.
  36. *
  37. * This is a hook provided by the tripal_chado module for
  38. * offloading the hook_field_formatter_info() for each field
  39. * to specify.
  40. *
  41. */
  42. function chado_feature__residues_formatter_info() {
  43. return array(
  44. 'label' => t('Residues'),
  45. 'field types' => array('chado_feature__residues'),
  46. );
  47. }
  48. /**
  49. *
  50. * @param unknown $entity_type
  51. * @param unknown $entity
  52. * @param unknown $field
  53. * @param unknown $instance
  54. * @param unknown $langcode
  55. * @param unknown $items
  56. * @param unknown $display
  57. */
  58. function chado_feature__residues_formatter(&$element, $entity_type, $entity, $field,
  59. $instance, $langcode, $items, $display) {
  60. foreach ($items as $delta => $item) {
  61. $residues = key_exists('value', $item) ? $item['value'] : '';
  62. $content = '<pre class="residues-formatter">' . $residues . '</pre>';
  63. $element[$delta] = array(
  64. // We create a render array to produce the desired markup,
  65. '#type' => 'markup',
  66. '#markup' => $content,
  67. );
  68. }
  69. }
  70. /**
  71. *
  72. * @param unknown $field_name
  73. * @param unknown $widget
  74. * @param unknown $form
  75. * @param unknown $form_state
  76. * @param unknown $field
  77. * @param unknown $instance
  78. * @param unknown $langcode
  79. * @param unknown $items
  80. * @param unknown $delta
  81. * @param unknown $element
  82. */
  83. function chado_feature__residues_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  84. $widget['value'] = array(
  85. '#type' => 'textarea',
  86. '#title' => $element['#title'],
  87. '#description' => $element['#description'],
  88. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  89. '#default_value' => count($items) > 0 ? $items[0]['value'] : '',
  90. '#delta' => $delta,
  91. '#element_validate' => array('chado_feature__residues_widget_validate'),
  92. '#cols' => 30,
  93. );
  94. }
  95. /**
  96. * Callback function for validating the chado_feature__residues_widget.
  97. */
  98. function chado_feature__residues_widget_validate($element, &$form_state) {
  99. $field_name = $element['#parents'][0];
  100. // Remove any white spaces.
  101. $residues = tripal_chado_get_field_form_values($field_name, $form_state);
  102. if ($residues) {
  103. $residues = preg_replace('/\s/', '', $residues);
  104. tripal_chado_set_field_form_values($field_name, $form_state, $residues);
  105. }
  106. }