data__sequence_length_widget.inc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. class data__sequence_length_widget extends TripalFieldWidget {
  3. // The default lable for this field.
  4. public static $label = 'Sequence length';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('data__sequence_length');
  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. $field_name = $this->field['field_name'];
  14. $field_type = $this->field['type'];
  15. $field_table = $this->field['settings']['chado_table'];
  16. $field_column = $this->field['settings']['chado_column'];
  17. $widget['value'] = array(
  18. '#type' => 'value',
  19. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  20. );
  21. $widget['chado-feature__seqlen'] = array(
  22. '#type' => 'value',
  23. '#value' => 0,
  24. '#title' => $element['#title'],
  25. '#description' => $element['#description'],
  26. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  27. '#delta' => $delta,
  28. );
  29. }
  30. /**
  31. * Performs validation of the widgetForm.
  32. *
  33. * Use this validate to ensure that form values are entered correctly. Note
  34. * this is different from the validate() function which ensures that the
  35. * field data meets expectations.
  36. *
  37. * @param $form
  38. * @param $form_state
  39. */
  40. public function validate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  41. }
  42. /**
  43. *
  44. * @see TripalFieldWidget::submit()
  45. */
  46. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  47. $field_name = $this->field['field_name'];
  48. $field_type = $this->field['type'];
  49. $field_table = $this->field['settings']['chado_table'];
  50. $field_column = $this->field['settings']['chado_column'];
  51. // Get the residues so we can calculate teh length.
  52. $residues = $form_state['values']['feature__residues']['und'][0]['chado-feature__residues'];
  53. // Remove any white spaces.
  54. if ($residues) {
  55. $residues = preg_replace('/\s/', '', $residues);
  56. $form_state['values']['feature__residues']['und'][0]['chado-feature__residues'] = $residues;
  57. $items[0]['chado-feature__seqlen'] = strlen($residues);
  58. }
  59. else {
  60. $items[0]['chado-feature__seqlen'] = '__NULL__';
  61. }
  62. }
  63. }