uo__unit_widget.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. class uo__unit_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Unit';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['uo__unit'];
  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. $unittype_id = 0;
  18. if (count($items) > 0 and array_key_exists('chado-' . $field_table . '__unittype_id', $items[0])) {
  19. $unittype_id = $items[0]['chado-' . $field_table . '__unittype_id'];
  20. }
  21. $widget['value'] = [
  22. '#type' => 'value',
  23. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  24. ];
  25. $cv = chado_get_cv(['name' => 'featuremap_units']);
  26. $options = chado_get_cvterm_select_options($cv->cv_id);
  27. unset($options[0]);
  28. $widget['chado-' . $field_table . '__unittype_id'] = [
  29. '#type' => 'select',
  30. '#title' => $element['#title'],
  31. '#description' => $element['#description'],
  32. '#options' => $options,
  33. '#default_value' => $unittype_id,
  34. '#empty_option' => '- Select a Unit -',
  35. '#required' => $element['#required'],
  36. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  37. '#delta' => $delta,
  38. ];
  39. }
  40. /**
  41. * @see TripalFieldWidget::validate()
  42. */
  43. public function validate($element, $form, &$form_state, $langcode, $delta) {
  44. $field_name = $this->field['field_name'];
  45. $field_table = $this->instance['settings']['chado_table'];
  46. // Make sure the value is set to the organism_id
  47. $unittype_id = $form_state['values'][$field_name]['und'][0]['chado-' . $field_table . '__unittype_id'];
  48. $form_state['values'][$field_name]['und'][0]['value'] = $unittype_id;
  49. }
  50. }