data__sequence_widget.inc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 = ['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'] = [
  28. '#type' => 'value',
  29. '#value' => $residues,
  30. ];
  31. $widget['chado-' . $field_table . '__' . $field_column] = [
  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. }