data__sequence_length_widget.inc

File

tripal_chado/includes/TripalFields/data__sequence_length/data__sequence_length_widget.inc
View source
  1. <?php
  2. class data__sequence_length_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_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->instance['settings']['chado_table'];
  16. $field_column = $this->instance['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. *
  32. * @see TripalFieldWidget::submit()
  33. */
  34. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  35. $field_name = $this->field['field_name'];
  36. $field_type = $this->field['type'];
  37. $field_table = $this->instance['settings']['chado_table'];
  38. $field_column = $this->instance['settings']['chado_column'];
  39. // Get the residues so we can calculate the length.
  40. if ($form_state['values']['data__sequence']['und'][0]['chado-feature__residues']){
  41. $residues = $form_state['values']['data__sequence']['und'][0]['chado-feature__residues'];
  42. // Remove spaces and new lines from the residues string.
  43. $residues = preg_replace('/\s/', '', $residues);
  44. $length = strlen($residues);
  45. $form_state['values'][$field_name]['und'][0]['chado-feature__seqlen'] = $length;
  46. $form_state['values'][$field_name]['und'][0]['value'] = $length;
  47. }
  48. else {
  49. // Otherwise, remove the md5 value
  50. $form_state['values'][$field_name]['und'][0]['chado-feature__seqlen'] = '__NULL__';
  51. }
  52. }
  53. }