sep__protocol_widget.inc

File

tripal_chado/includes/TripalFields/sep__protocol/sep__protocol_widget.inc
View source
  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. $linker_field = 'chado-' . $field_table . '__protocol_id';
  77. $protocols = [];
  78. //options are all protocols
  79. //It could be argued that options should only be protocols where protocol_type matches the bundle base table.
  80. $sql = "SELECT * FROM {protocol}";
  81. $results = chado_query($sql);
  82. foreach ($results as $protocol) {
  83. $protocols[$protocol->protocol_id] = $protocol->name;
  84. }
  85. $widget['value'] = [
  86. '#type' => 'select',
  87. '#title' => $element['#title'],
  88. '#description' => $element['#description'],
  89. '#options' => $protocols,
  90. '#empty_option' => '- Select a Protocol -',
  91. '#required' => $element['#required'],
  92. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  93. '#delta' => $delta,
  94. ];
  95. }
  96. /**
  97. * Performs validation of the widgetForm.
  98. *
  99. * Use this validate to ensure that form values are entered correctly.
  100. * The 'value' key of this field must be set in the $form_state['values']
  101. * array anytime data is entered by the user. It may be the case that there
  102. * are other fields for helping select a value. In the end those helper
  103. * fields must be used to set the 'value' field.
  104. */
  105. public function validate($element, $form, &$form_state, $langcode, $delta) {
  106. }
  107. /**
  108. * Performs extra commands when the entity form is submitted.
  109. *
  110. * Drupal typically does not provide a submit hook for fields. The
  111. * TripalField provides one to allow for behind-the-scenes actions to
  112. * occur. This function should never be used for updates, deletes or
  113. * inserts for the Chado table associated with the field. Rather, the
  114. * storage backend should be allowed to handle inserts, updates deletes.
  115. * However, it is permissible to perform inserts, updates or deletions within
  116. * Chado using this function. Those operations can be performed if needed but
  117. * on other tables not directly associated with the field.
  118. *
  119. * An example is the chado.feature_synonym table. The chado_linker__synonym
  120. * field allows the user to provide a brand new synonynm and it must add it
  121. * to the chado.synonym table prior to the record in the
  122. * chado.feature_synonym table. This insert occurs in the widgetFormSubmit
  123. * function.
  124. *
  125. * @param $form
  126. * The submitted form array.
  127. * @param $form_state .
  128. * The form state array.
  129. * @param $entity_type
  130. * The type of $entity.
  131. * @param $entity
  132. * The entity for the operation.
  133. * @param $langcode
  134. * The language associated with $items.
  135. * @param $delta
  136. */
  137. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  138. }
  139. }