remote__data_widget.inc

File

tripal_ws/includes/TripalFields/remote__data/remote__data_widget.inc
View source
  1. <?php
  2. class remote__data_widget extends WebServicesFieldWidget {
  3. // The default label for this field.
  4. public static $default_label = 'Remote Data';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('remote__data');
  7. /**
  8. * Provides the form for editing of this field.
  9. *
  10. * This function corresponds to the hook_field_widget_form()
  11. * function of the Drupal Field API.
  12. *
  13. * This form is diplayed when the user creates a new entity or edits an
  14. * existing entity. If the field is attached to the entity then the form
  15. * provided by this function will be displayed.
  16. *
  17. * At a minimum, the form must have a 'value' element. For Tripal, the
  18. * 'value' element of a field always corresponds to the value that is
  19. * presented to the end-user either directly on the page (with formatting)
  20. * or via web services, or some other mechanism. However, the 'value' is
  21. * sometimes not enough for a field. For example, the Tripal Chado module
  22. * maps fields to table columns and sometimes those columns are foreign keys
  23. * therefore, the Tripal Chado modules does not just use the 'value' but adds
  24. * additional elements to help link records via FKs. But even in this case
  25. * the 'value' element must always be present in the return form and in such
  26. * cases it's value should be set equal to that added in the 'load' function.
  27. *
  28. * @param $widget
  29. * @param $form
  30. * The form structure where widgets are being attached to. This might be a
  31. * full form structure, or a sub-element of a larger form.
  32. * @param $form_state
  33. * An associative array containing the current state of the form.
  34. * @param $langcode
  35. * The language associated with $items.
  36. * @param $items
  37. * Array of default values for this field.
  38. * @param $delta
  39. * The order of this item in the array of subelements (0, 1, 2, etc).
  40. * @param $element
  41. * A form element array containing basic properties for the widget:
  42. * - #entity_type: The name of the entity the field is attached to.
  43. * - #bundle: The name of the field bundle the field is contained in.
  44. * - #field_name: The name of the field.
  45. * - #language: The language the field is being edited in.
  46. * - #field_parents: The 'parents' space for the field in the form. Most
  47. * widgets can simply overlook this property. This identifies the location
  48. * where the field values are placed within $form_state['values'], and is
  49. * used to access processing information for the field through the
  50. * field_form_get_state() and field_form_set_state() functions.
  51. * - #columns: A list of field storage columns of the field.
  52. * - #title: The sanitized element label for the field instance, ready for
  53. * output.
  54. * - #description: The sanitized element description for the field instance,
  55. * ready for output.
  56. * - #required: A Boolean indicating whether the element value is required;
  57. * for required multiple value fields, only the first widget's values are
  58. * required.
  59. * - #delta: The order of this item in the array of subelements; see
  60. * $delta above
  61. */
  62. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  63. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  64. // Get the field settings.
  65. /* $field_name = $this->field['field_name'];
  66. $field_type = $this->field['type'];
  67. // Get the setting for the option for how this widget.
  68. $instance = $this->instance;
  69. $settings = '';
  70. $site_list = '';
  71. $tokens = array();
  72. // Get the form info from the bundle about to be saved.
  73. $bundle_info = tripal_load_bundle_entity(array('name' => $form_state['build_info']['args']['0']['bundle']));
  74. // Retrieve all available tokens.
  75. $tokens = tripal_get_entity_tokens($bundle_info);
  76. // If the field already has a value then it will come through the $items
  77. // array. This happens when editing an existing record.
  78. // FORM PROPER
  79. $widget['#prefix'] = "<span id='$field_name-remote_data-$delta'>";
  80. $widget['#suffix'] = "</span>";
  81. $widget['value'] = array(
  82. '#type' => 'value',
  83. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  84. );
  85. $widget['data_info'] = array(
  86. '#type' => 'fieldset',
  87. '#title' => 'Remote Data Settings',
  88. '#description' => 'Provide the site name, query and description for the remote data source.',
  89. '#collapsible' => TRUE,
  90. '#collapsed' => FALSE,
  91. '#prefix' => "<div id='set_titles-fieldset'>",
  92. '#suffix' => '</div>',
  93. );
  94. // Get the site info from the tripal_sites table.
  95. // Get the field groups associated with this bundle.
  96. $sites = db_select('tripal_sites', 's')
  97. ->fields('s')
  98. ->execute()->fetchAll();
  99. foreach ($sites as $site) {
  100. $rows[] = $site->name;
  101. }
  102. $widget['data_info']['site'] = array(
  103. '#type' => 'select',
  104. '#title' => t('Site'),
  105. '#options' => $rows,
  106. '#default_value' => $site_list,
  107. );
  108. $widget['data_info']['query'] = array(
  109. '#type' => 'textarea',
  110. '#title' => 'Query',
  111. '#description' => 'Build the query string that should be appended after the url. The tokens
  112. listed below may be used in your query build.',
  113. '#default_value' => $this->instance['settings']['data_info']['query'],
  114. '#rows' => 5
  115. );
  116. $widget['set_titles']['token_display']['tokens'] = array(
  117. '#type' => 'hidden',
  118. '#value' => serialize($tokens)
  119. );
  120. $widget['data_info']['token_display'] = array(
  121. '#type' => 'fieldset',
  122. '#title' => 'Available Tokens',
  123. '#description' => 'Copy the token and paste it into the "Query" text field above.',
  124. '#collapsible' => TRUE,
  125. '#collapsed' => TRUE
  126. );
  127. $widget['data_info']['token_display']['content'] = array(
  128. '#type' => 'item',
  129. '#markup' => theme_token_list($tokens),
  130. );
  131. $widget['data_info']['description'] = array(
  132. '#type' => 'textarea',
  133. '#title' => 'Description',
  134. '#description' => 'Describe the data being pulled in.',
  135. '#default_value' => $this->instance['settings']['data_info']['description'],
  136. '#rows' => 1
  137. );
  138. */
  139. //TODO Add test button to ensure query returns info.
  140. }
  141. /**
  142. * Performs validation of the widgetForm.
  143. *
  144. * Use this validate to ensure that form values are entered correctly.
  145. * The 'value' key of this field must be set in the $form_state['values']
  146. * array anytime data is entered by the user. It may be the case that there
  147. * are other fields for helping select a value. In the end those helper
  148. * fields must be used to set the 'value' field.
  149. */
  150. public function validate($element, $form, &$form_state, $langcode, $delta) {
  151. //TODO validate the tokens, site, and query. Test that query returns data.
  152. }
  153. }