TripalFieldWidget.inc

File

tripal/includes/TripalFields/TripalFieldWidget.inc
View source
  1. <?php
  2. class TripalFieldWidget {
  3. // The default lable for this field.
  4. public static $default_label = 'Tripal Field.';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('no_widget');
  7. /**
  8. * Instantiates a new TripalFieldWidget object.
  9. *
  10. * @param $field
  11. * An array containing the field data as returned by field_info_field()
  12. * @param $instance
  13. * (Optional). Set the instance of this field when one is available. This
  14. * is necessary when working with instance specific functions such as the
  15. * formatterSettingsForm, widgetForm, etc.
  16. */
  17. public function __construct($field, $instance = NULL) {
  18. $this->field = $field;
  19. $this->instance = $instance;
  20. }
  21. /**
  22. * Provides information about the widgets provided by this field.
  23. *
  24. * This function corresponds to the hook_field_widget_info() function of
  25. * the Drupal Field API.
  26. *
  27. * This is a static function as it provides default values for all of the
  28. * widgets for this field type, and thus we don't need an instantiated
  29. * object to provide this information.
  30. *
  31. * @return
  32. * An associative array with key/value pairs compatible with those from the
  33. * hook_field_widget_info() function of the Drupal Field API.
  34. */
  35. public static function info() {
  36. $class = get_called_class();
  37. return array(
  38. 'label' => $class::$default_label,
  39. 'field types' => $class::$field_types,
  40. );
  41. }
  42. /**
  43. * Provides the form for editing of this field.
  44. *
  45. * This function corresponds to the hook_field_widget_form()
  46. * function of the Drupal Field API.
  47. *
  48. * This form is diplayed when the user creates a new entity or edits an
  49. * existing entity. If the field is attached to the entity then the form
  50. * provided by this function will be displayed.
  51. *
  52. * At a minimum, the form must have a 'value' element. For Tripal, the
  53. * 'value' element of a field always corresponds to the value that is
  54. * presented to the end-user either directly on the page (with formatting)
  55. * or via web services, or some other mechanism. However, the 'value' is
  56. * sometimes not enough for a field. For example, the Tripal Chado module
  57. * maps fields to table columns and sometimes those columns are foreign keys
  58. * therefore, the Tripal Chado modules does not just use the 'value' but adds
  59. * additional elements to help link records via FKs. But even in this case
  60. * the 'value' element must always be present in the return form and in such
  61. * cases it's value should be set equal to that added in the 'load' function.
  62. *
  63. * @param $widget
  64. * @param $form
  65. * The form structure where widgets are being attached to. This might be a
  66. * full form structure, or a sub-element of a larger form.
  67. * @param $form_state
  68. * An associative array containing the current state of the form.
  69. * @param $langcode
  70. * The language associated with $items.
  71. * @param $items
  72. * Array of default values for this field.
  73. * @param $delta
  74. * The order of this item in the array of subelements (0, 1, 2, etc).
  75. * @param $element
  76. * A form element array containing basic properties for the widget:
  77. * - #entity_type: The name of the entity the field is attached to.
  78. * - #bundle: The name of the field bundle the field is contained in.
  79. * - #field_name: The name of the field.
  80. * - #language: The language the field is being edited in.
  81. * - #field_parents: The 'parents' space for the field in the form. Most
  82. * widgets can simply overlook this property. This identifies the location
  83. * where the field values are placed within $form_state['values'], and is
  84. * used to access processing information for the field through the
  85. * field_form_get_state() and field_form_set_state() functions.
  86. * - #columns: A list of field storage columns of the field.
  87. * - #title: The sanitized element label for the field instance, ready for
  88. * output.
  89. * - #description: The sanitized element description for the field instance,
  90. * ready for output.
  91. * - #required: A Boolean indicating whether the element value is required;
  92. * for required multiple value fields, only the first widget's values are
  93. * required.
  94. * - #delta: The order of this item in the array of subelements; see
  95. * $delta above
  96. */
  97. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  98. $widget['value'] = array(
  99. '#type' => 'value',
  100. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  101. );
  102. $widget['#field'] = $this->field;
  103. $widget['#instance'] = $this->instance;
  104. $widget['#element_validate'] = array('tripal_field_widget_form_validate');
  105. $widget['#theme'] = 'tripal_field_default';
  106. }
  107. /**
  108. * Performs validation of the widget form.
  109. *
  110. * Use this validate to ensure that form values are entered correctly.
  111. * The 'value' key of this field must be set in the $form_state['values']
  112. * array anytime data is entered by the user. It may be the case that there
  113. * are other fields for helping select a value. In the end those helper
  114. * fields must be used to set the 'value' field.
  115. */
  116. public function validate($element, $form, &$form_state, $langcode, $delta) {
  117. }
  118. /**
  119. * Performs extra commands when the entity form is submitted.
  120. *
  121. * Drupal typically does not provide a submit hook for fields. The
  122. * TripalField provides one to allow for behind-the-scenes actions to
  123. * occur. This function should never be used for updates, deletes or
  124. * inserts for the Chado table associated with the field. Rather, the
  125. * storage backend should be allowed to handle inserts, updates deletes.
  126. * However, it is permissible to perform inserts, updates or deletions within
  127. * Chado using this function. Those operations can be performed if needed but
  128. * on other tables not directly associated with the field.
  129. *
  130. * An example is the chado.feature_synonym table. The chado_linker__synonym
  131. * field allows the user to provide a brand new synonynm and it must add it
  132. * to the chado.synonym table prior to the record in the
  133. * chado.feature_synonym table. This insert occurs in the widgetFormSubmit
  134. * function.
  135. *
  136. * @param $entity_type
  137. * The type of $entity.
  138. * @param $entity
  139. * The entity for the operation.
  140. * @param $field
  141. * The field structure for the operation.
  142. * @param $instance
  143. * The instance structure for $field on $entity's bundle.
  144. * @param $langcode
  145. * The language associated with $items.
  146. * @param $items
  147. * $entity->{$field['field_name']}[$langcode], or an empty array if unset.
  148. * @param $form
  149. * The submitted form array.
  150. * @param $form_state.
  151. * The form state array.
  152. */
  153. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  154. }
  155. /**
  156. * The theme function for the widget.
  157. *
  158. * @param $element
  159. * A form element array containing basic properties for the widget:
  160. * - #entity_type: The name of the entity the field is attached to.
  161. * - #bundle: The name of the field bundle the field is contained in.
  162. * - #field_name: The name of the field.
  163. * - #language: The language the field is being edited in.
  164. * - #field_parents: The 'parents' space for the field in the form. Most
  165. * widgets can simply overlook this property. This identifies the location
  166. * where the field values are placed within $form_state['values'], and is
  167. * used to access processing information for the field through the
  168. * field_form_get_state() and field_form_set_state() functions.
  169. * - #columns: A list of field storage columns of the field.
  170. * - #title: The sanitized element label for the field instance, ready for
  171. * output.
  172. * - #description: The sanitized element description for the field instance,
  173. * ready for output.
  174. * - #required: A Boolean indicating whether the element value is required;
  175. * for required multiple value fields, only the first widget's values are
  176. * required.
  177. * - #delta: The order of this item in the array of subelements; see
  178. * $delta above
  179. *
  180. * @return
  181. * A drupal renderable array or HTML or an empty string if no
  182. * theming is to be applied.
  183. */
  184. public function theme($element) {
  185. }
  186. }