sio__vocabulary_widget.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. class sio__vocabulary_widget extends ChadoFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Vocabulary';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('sio__vocabulary');
  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. // If the items array is empty then we are creating a new entity.
  18. // Since this is set when the entity type is created, we don't want to allow
  19. // content managers to change it. Thus we need to look up the value for the
  20. // entity type and use it here.
  21. if (empty($items)) {
  22. // Use the bundle to get the cv_id choosen for this cvterm-based entity.
  23. // ASSUMPTION: the cv_id is saved as the "type_value" of the bundle.
  24. $bundle = tripal_load_bundle_entity(array('name' => $widget['#bundle']));
  25. $cv = chado_get_cv(array('cv_id' => $bundle->type_value));
  26. // Now populate the items array with defaults based on the cv.
  27. if ($cv) {
  28. $items[$delta] = array(
  29. 'value' => $cv->name,
  30. 'chado-' . $field_table . '__cv_id' => $cv->cv_id,
  31. );
  32. }
  33. else {
  34. tripal_report_error(
  35. $field_name,
  36. TRIPAL_ERROR,
  37. 'Unable to determine default vocabulary for :name Tripal Content Type',
  38. array(':name' => $bundle->label)
  39. );
  40. drupal_set_message(t('Unable to determine default vocabulary for :name Tripal Content Type',
  41. array(':name' => $bundle->label)), 'error');
  42. }
  43. }
  44. $widget['value'] = array(
  45. '#type' => 'value',
  46. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  47. );
  48. $widget['chado-' . $field_table . '__cv_id'] = array(
  49. '#type' => 'value',
  50. '#value' => array_key_exists($delta, $items) ? $items[$delta]['chado-' . $field_table . '__cv_id'] : '',
  51. );
  52. $widget['vocabulary_name'] = array(
  53. '#type' => 'item',
  54. '#title' => 'Vocabulary',
  55. '#markup' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  56. );
  57. }
  58. /**
  59. * @see TripalFieldWidget::validate()
  60. */
  61. public function validate($element, $form, &$form_state, $langcode, $delta) {
  62. $field_name = $this->field['field_name'];
  63. $field_table = $this->instance['settings']['chado_table'];
  64. // Make sure the value is set to the cv_id
  65. $cv_id = $form_state['values'][$field_name][$langcode][0]['chado-' . $field_table . '__cv_id'];
  66. $form_state['values'][$field_name][$langcode][0]['value'] = $cv_id;
  67. }
  68. }