sep__protocol_widget.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @class
  4. * Purpose:
  5. *
  6. * Data:
  7. * Assumptions:
  8. */
  9. class sep__protocol_widget extends ChadoFieldWidget {
  10. // The default label for this field.
  11. public static $default_label = 'Protocol';
  12. // The list of field types for which this formatter is appropriate.
  13. public static $field_types = ['sep__protocol'];
  14. /**
  15. * Provides the form for editing of this field.
  16. *
  17. * This function corresponds to the hook_field_widget_form()
  18. * function of the Drupal Field API.
  19. *
  20. * This form is diplayed when the user creates a new entity or edits an
  21. * existing entity. If the field is attached to the entity then the form
  22. * provided by this function will be displayed.
  23. *
  24. * At a minimum, the form must have a 'value' element. For Tripal, the
  25. * 'value' element of a field always corresponds to the value that is
  26. * presented to the end-user either directly on the page (with formatting)
  27. * or via web services, or some other mechanism. However, the 'value' is
  28. * sometimes not enough for a field. For example, the Tripal Chado module
  29. * maps fields to table columns and sometimes those columns are foreign keys
  30. * therefore, the Tripal Chado modules does not just use the 'value' but adds
  31. * additional elements to help link records via FKs. But even in this case
  32. * the 'value' element must always be present in the return form and in such
  33. * cases it's value should be set equal to that added in the 'load' function.
  34. *
  35. * @param $widget
  36. * @param $form
  37. * The form structure where widgets are being attached to. This might be a
  38. * full form structure, or a sub-element of a larger form.
  39. * @param $form_state
  40. * An associative array containing the current state of the form.
  41. * @param $langcode
  42. * The language associated with $items.
  43. * @param $items
  44. * Array of default values for this field.
  45. * @param $delta
  46. * The order of this item in the array of subelements (0, 1, 2, etc).
  47. * @param $element
  48. * A form element array containing basic properties for the widget:
  49. * - #entity_type: The name of the entity the field is attached to.
  50. * - #bundle: The name of the field bundle the field is contained in.
  51. * - #field_name: The name of the field.
  52. * - #language: The language the field is being edited in.
  53. * - #field_parents: The 'parents' space for the field in the form. Most
  54. * widgets can simply overlook this property. This identifies the location
  55. * where the field values are placed within $form_state['values'], and is
  56. * used to access processing information for the field through the
  57. * field_form_get_state() and field_form_set_state() functions.
  58. * - #columns: A list of field storage columns of the field.
  59. * - #title: The sanitized element label for the field instance, ready for
  60. * output.
  61. * - #description: The sanitized element description for the field instance,
  62. * ready for output.
  63. * - #required: A Boolean indicating whether the element value is required;
  64. * for required multiple value fields, only the first widget's values are
  65. * required.
  66. * - #delta: The order of this item in the array of subelements; see
  67. * $delta above
  68. */
  69. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  70. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  71. $settings = $this->field['settings'];
  72. $field_name = $this->field['field_name'];
  73. $field_type = $this->field['type'];
  74. $field_table = $this->instance['settings']['chado_table'];
  75. $field_column = $this->instance['settings']['chado_column'];
  76. $base_table = $this->instance['settings']['base_table'];
  77. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  78. $protocol_term = chado_get_semweb_term($field_column, $field_table);
  79. $protocol_name_term = chado_get_semweb_term('protocol', 'name');
  80. $protocol_type_term = chado_get_semweb_term('protocol', 'type_id');
  81. // Set defaults for the form elements.
  82. $protocol_name = '';
  83. $protocol_id = '';
  84. // If the field already has a value then it will come through the $items
  85. // array. This happens when editing an existing record.
  86. if (count($items) > 0 and array_key_exists($delta, $items)) {
  87. $protocol_name = array_key_exists($protocol_name_term, $items[$delta]['value']) ? $items[$delta]['value'][$protocol_name_term] : $protocol_name;
  88. }
  89. // Get the list of protocols
  90. $protocols = [];
  91. $sql = "SELECT * FROM {protocol}";
  92. $results = chado_query($sql);
  93. foreach ($results as $protocol) {
  94. $protocols[$protocol->protocol_id] = $protocol->name;
  95. if ($protocol->name == $protocol_name) {
  96. $protocol_id = $protocol->protocol_id;
  97. }
  98. }
  99. $widget['value'] = array(
  100. '#type' => 'value',
  101. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  102. );
  103. $widget[$linker_field] = [
  104. '#type' => 'select',
  105. '#title' => $element['#title'],
  106. '#description' => $element['#description'],
  107. '#options' => $protocols,
  108. '#empty_option' => '- Select a Protocol -',
  109. '#required' => $element['#required'],
  110. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  111. '#delta' => $delta,
  112. '#default_value' => $protocol_id,
  113. ];
  114. }
  115. /**
  116. * @see TripalFieldWidget::validate()
  117. */
  118. public function validate($element, $form, &$form_state, $langcode, $delta) {
  119. $field_name = $this->field['field_name'];
  120. $field_table = $this->instance['settings']['chado_table'];
  121. $field_column = $this->instance['settings']['chado_column'];
  122. $linker_field = 'chado-' . $field_table . '__' . $field_column;
  123. // Make sure the value is set to the organism_id
  124. $protocol_id = $form_state['values'][$field_name]['und'][0][$linker_field];
  125. $form_state['values'][$field_name]['und'][0]['value'] = $protocol_id;
  126. }
  127. /**
  128. * @see TripalFieldWidget::submit()
  129. */
  130. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  131. }
  132. }