data__sequence_widget.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. class data__sequence_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Sequence';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('data__sequence');
  7. /**
  8. *
  9. * @see TripalFieldWidget::form()
  10. */
  11. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  12. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  13. $settings = $this->field['settings'];
  14. $field_name = $this->field['field_name'];
  15. $field_type = $this->field['type'];
  16. $field_table = $this->instance['settings']['chado_table'];
  17. $field_column = $this->instance['settings']['chado_column'];
  18. // Get the field defaults.
  19. $residues = '';
  20. if (count($items) > 0 and array_key_exists('value', $items[0])) {
  21. $residues = $items[0]['value'];
  22. }
  23. if (array_key_exists('values', $form_state) and
  24. array_key_exists($field_name, $form_state['values'])) {
  25. $residues = $form_state['values'][$field_name][$langcode][$delta]['value'];
  26. }
  27. $widget['value'] = array(
  28. '#type' => 'value',
  29. '#value' => $residues,
  30. );
  31. $widget['chado-' . $field_table . '__' . $field_column] = array(
  32. '#type' => 'textarea',
  33. '#title' => $element['#title'],
  34. '#description' => $element['#description'],
  35. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  36. '#default_value' => $residues,
  37. '#delta' => $delta,
  38. '#cols' => 30,
  39. );
  40. }
  41. /**
  42. * @see TripalFieldWidget::validateDefaults()
  43. */
  44. public function validateDefaults($element, $form, &$form_state, $langcode, $delta) {
  45. $field_name = $this->field['field_name'];
  46. $field_table = $this->instance['settings']['chado_table'];
  47. $field_column = $this->instance['settings']['chado_column'];
  48. $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $field_column];
  49. $form_state['values'][$field_name]['und'][$delta]['value'] = $value;
  50. }
  51. /**
  52. *
  53. * @see TripalFieldWidget::submit()
  54. */
  55. public function validate($element, $form, &$form_state, $langcode, $delta) {
  56. $field_name = $this->field['field_name'];
  57. $field_table = $this->instance['settings']['chado_table'];
  58. $field_column = $this->instance['settings']['chado_column'];
  59. // Remove any white spaces.
  60. $residues = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $field_column];
  61. if ($residues) {
  62. $residues = preg_replace('/\s/', '', $residues);
  63. $form_state['values'][$field_name]['und'][$delta]['value'] = $residues;
  64. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $field_column] = $residues;
  65. }
  66. // If the residue information has been removed then we want to signal such.
  67. // When it's removed the value != residues but if it was never set then they're both empty.
  68. elseif (!empty($form_state['values'][$field_name]['und'][$delta]['value'])) {
  69. $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
  70. $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $field_column] = '';
  71. }
  72. }
  73. }