sep__protocol_widget.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @class
  4. * Purpose:
  5. *
  6. * Allowing edit?
  7. * Data:
  8. * Assumptions:
  9. */
  10. class sep__protocol_widget extends ChadoFieldWidget {
  11. // The default label for this field.
  12. public static $default_label = 'Protocol';
  13. // The list of field types for which this formatter is appropriate.
  14. public static $field_types = ['sep__protocol'];
  15. /**
  16. * Provides the form for editing of this field.
  17. *
  18. * This function corresponds to the hook_field_widget_form()
  19. * function of the Drupal Field API.
  20. *
  21. * This form is diplayed when the user creates a new entity or edits an
  22. * existing entity. If the field is attached to the entity then the form
  23. * provided by this function will be displayed.
  24. *
  25. * At a minimum, the form must have a 'value' element. For Tripal, the
  26. * 'value' element of a field always corresponds to the value that is
  27. * presented to the end-user either directly on the page (with formatting)
  28. * or via web services, or some other mechanism. However, the 'value' is
  29. * sometimes not enough for a field. For example, the Tripal Chado module
  30. * maps fields to table columns and sometimes those columns are foreign keys
  31. * therefore, the Tripal Chado modules does not just use the 'value' but adds
  32. * additional elements to help link records via FKs. But even in this case
  33. * the 'value' element must always be present in the return form and in such
  34. * cases it's value should be set equal to that added in the 'load' function.
  35. *
  36. * @param $widget
  37. * @param $form
  38. * The form structure where widgets are being attached to. This might be a
  39. * full form structure, or a sub-element of a larger form.
  40. * @param $form_state
  41. * An associative array containing the current state of the form.
  42. * @param $langcode
  43. * The language associated with $items.
  44. * @param $items
  45. * Array of default values for this field.
  46. * @param $delta
  47. * The order of this item in the array of subelements (0, 1, 2, etc).
  48. * @param $element
  49. * A form element array containing basic properties for the widget:
  50. * - #entity_type: The name of the entity the field is attached to.
  51. * - #bundle: The name of the field bundle the field is contained in.
  52. * - #field_name: The name of the field.
  53. * - #language: The language the field is being edited in.
  54. * - #field_parents: The 'parents' space for the field in the form. Most
  55. * widgets can simply overlook this property. This identifies the location
  56. * where the field values are placed within $form_state['values'], and is
  57. * used to access processing information for the field through the
  58. * field_form_get_state() and field_form_set_state() functions.
  59. * - #columns: A list of field storage columns of the field.
  60. * - #title: The sanitized element label for the field instance, ready for
  61. * output.
  62. * - #description: The sanitized element description for the field instance,
  63. * ready for output.
  64. * - #required: A Boolean indicating whether the element value is required;
  65. * for required multiple value fields, only the first widget's values are
  66. * required.
  67. * - #delta: The order of this item in the array of subelements; see
  68. * $delta above
  69. */
  70. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  71. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  72. $settings = $this->field['settings'];
  73. $field_name = $this->field['field_name'];
  74. $field_type = $this->field['type'];
  75. $field_table = $this->instance['settings']['chado_table'];
  76. $field_column = $this->instance['settings']['chado_column'];
  77. $linker_field = 'chado-' . $field_table . '__protocol_id';
  78. $protocols = [];
  79. //options are all protocols
  80. //It could be argued that options should only be protocols where protocol_type matches the bundle base table.
  81. $sql = "SELECT * FROM {protocol}";
  82. $results = chado_query($sql);
  83. foreach ($results as $protocol) {
  84. $protocols[$protocol->protocol_id] = $protocol->name;
  85. }
  86. $widget['value'] = [
  87. '#type' => 'select',
  88. '#title' => $element['#title'],
  89. '#description' => $element['#description'],
  90. '#options' => $protocols,
  91. '#empty_option' => '- Select a Protocol -',
  92. '#required' => $element['#required'],
  93. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  94. '#delta' => $delta,
  95. ];
  96. }
  97. /**
  98. * Performs validation of the widgetForm.
  99. *
  100. * Use this validate to ensure that form values are entered correctly.
  101. * The 'value' key of this field must be set in the $form_state['values']
  102. * array anytime data is entered by the user. It may be the case that there
  103. * are other fields for helping select a value. In the end those helper
  104. * fields must be used to set the 'value' field.
  105. */
  106. public function validate($element, $form, &$form_state, $langcode, $delta) {
  107. }
  108. /**
  109. * Performs extra commands when the entity form is submitted.
  110. *
  111. * Drupal typically does not provide a submit hook for fields. The
  112. * TripalField provides one to allow for behind-the-scenes actions to
  113. * occur. This function should never be used for updates, deletes or
  114. * inserts for the Chado table associated with the field. Rather, the
  115. * storage backend should be allowed to handle inserts, updates deletes.
  116. * However, it is permissible to perform inserts, updates or deletions within
  117. * Chado using this function. Those operations can be performed if needed but
  118. * on other tables not directly associated with the field.
  119. *
  120. * An example is the chado.feature_synonym table. The chado_linker__synonym
  121. * field allows the user to provide a brand new synonynm and it must add it
  122. * to the chado.synonym table prior to the record in the
  123. * chado.feature_synonym table. This insert occurs in the widgetFormSubmit
  124. * function.
  125. *
  126. * @param $form
  127. * The submitted form array.
  128. * @param $form_state .
  129. * The form state array.
  130. * @param $entity_type
  131. * The type of $entity.
  132. * @param $entity
  133. * The entity for the operation.
  134. * @param $langcode
  135. * The language associated with $items.
  136. * @param $delta
  137. */
  138. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  139. }
  140. }