operation__analysis_widget.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. class operation__analysis_widget extends ChadoFieldWidget {
  3. // The default label for this field.
  4. public static $default_label = 'Analysis';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['operation__analysis'];
  7. /**
  8. * @see TripalFieldWidget::form()
  9. */
  10. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  11. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  12. $settings = $this->field['settings'];
  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. // Set the linker field appropriately.
  18. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  19. $analysis_id = 0;
  20. if (count($items) > 0 and array_key_exists($linker_field, $items[0])) {
  21. $analysis_id = $items[0][$linker_field];
  22. }
  23. $widget['value'] = [
  24. '#type' => 'value',
  25. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  26. ];
  27. $sql = "SELECT analysis_id, name FROM {analysis} ORDER BY name";
  28. $results = chado_query($sql);
  29. $options = ['' => '- Select an analysis -'];
  30. while ($r = $results->fetchObject()) {
  31. $options[$r->analysis_id] = $r->name;
  32. }
  33. $widget[$linker_field] = [
  34. '#type' => 'select',
  35. '#title' => $element['#title'],
  36. '#description' => $element['#description'],
  37. '#options' => $options,
  38. '#default_value' => $analysis_id,
  39. '#required' => $element['#required'],
  40. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  41. '#delta' => $delta,
  42. ];
  43. }
  44. /**
  45. * @see TripalFieldWidget::validate()
  46. */
  47. public function validate($element, $form, &$form_state, $langcode, $delta) {
  48. $field_name = $this->field['field_name'];
  49. $field_type = $this->field['type'];
  50. $field_table = $this->instance['settings']['chado_table'];
  51. $field_column = $this->instance['settings']['chado_column'];
  52. // Set the linker field appropriately.
  53. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  54. // Make sure the value is set to the organism_id
  55. $analysis_id = $form_state['values'][$field_name]['und'][0][$linker_field];
  56. $form_state['values'][$field_name]['und'][0]['value'] = $analysis_id;
  57. }
  58. }