remote__data_formatter.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. class remote__data_formatter extends WebServicesFieldFormatter {
  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. // The list of default settings for this formatter.
  8. public static $default_settings = array(
  9. 'setting1' => 'default_value',
  10. );
  11. /**
  12. * Provides the field's setting form.
  13. *
  14. * This function corresponds to the hook_field_formatter_settings_form()
  15. * function of the Drupal Field API.
  16. *
  17. * The settings form appears on the 'Manage Display' page of the content
  18. * type administration page. This function provides the form that will
  19. * appear on that page.
  20. *
  21. * To add a validate function, please create a static function in the
  22. * implementing class, and indicate that this function should be used
  23. * in the form array that is returned by this function.
  24. *
  25. * This form will not be displayed if the formatter_settings_summary()
  26. * function does not return anything.
  27. *
  28. * param $field
  29. * The field structure being configured.
  30. * param $instance
  31. * The instance structure being configured.
  32. * param $view_mode
  33. * The view mode being configured.
  34. * param $form
  35. * The (entire) configuration form array, which will usually have no use
  36. * here. Typically for reference only.
  37. * param $form_state
  38. * The form state of the (entire) configuration form.
  39. *
  40. * @return
  41. * A Drupal Form array containing the settings form for this field.
  42. */
  43. public function settingsForm($view_mode, $form, &$form_state) {
  44. }
  45. /**
  46. * Provides the display for a field
  47. *
  48. * This function corresponds to the hook_field_formatter_view()
  49. * function of the Drupal Field API.
  50. *
  51. * This function provides the display for a field when it is viewed on
  52. * the web page. The content returned by the formatter should only include
  53. * what is present in the $items[$delta]['values] array. This way, the
  54. * contents that are displayed on the page, via webservices and downloaded
  55. * into a CSV file will always be identical. The view need not show all
  56. * of the data in the 'values' array.
  57. *
  58. * @param $element
  59. * @param $entity_type
  60. * @param $entity
  61. * @param $langcode
  62. * @param $items
  63. * @param $display
  64. *
  65. * @return
  66. * An element array compatible with that returned by the
  67. * hook_field_formatter_view() function.
  68. */
  69. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  70. // Get the settings
  71. $settings = $display['settings'];
  72. $field_name = $this->field['field_name'];
  73. //Determine if single or multiple values returned.
  74. $count = count($items[0]['value']['members']);
  75. // Case #1: A single scalar value (string, number).
  76. if ($count == 1) {
  77. //Array key is based on the label, grab label to specify array in $items.
  78. $array_key = $items[0]['value']['members'][0]['label'];
  79. foreach($items[$array_key]['value'] as $index => $array_item){
  80. //Ignore all the extra ws info coming through.
  81. if($index == '@context' || $index == '@id' || $index == '@type'){
  82. continue;
  83. }
  84. else {
  85. $content = $array_item;
  86. }
  87. }
  88. $element[$array_key] = array(
  89. '#type' => 'markup',
  90. '#markup' => $content,
  91. );
  92. }
  93. /*
  94. * Case #2: An array of scalar values (1-level deep, e.g. the obi__organism
  95. * field on the gene or mRNA content type).
  96. * Case #3: An array of nested scalar values (2 or more levels deep. e.g.
  97. * the sbo__relationship field on all data types, but easily visible on mRNA content tpyes).
  98. */
  99. else if ($count > 1) {
  100. }
  101. //Check that the load function returned content.
  102. /*if (!empty($items[0]['value'])){
  103. $list = array();
  104. foreach ($items as $index => $item) {
  105. $list[$index] = $item['value'];
  106. }
  107. // If more than one value has been found display all values in an unordered
  108. // list.
  109. if (count($list) > 1) {
  110. $content = theme_item_list(array(
  111. 'items' => $list,
  112. 'title' => '',
  113. 'attributes' => array('class' => array($entity->bundle . '-remote-data-list', 'remote-data-field-list')),
  114. 'type' => 'ul'
  115. ));
  116. }
  117. else {
  118. $content = $list[0];
  119. }
  120. if(array_key_exists('members', $content)){
  121. $header = array($items[0]['value']['label']);
  122. $members = $content['members'];
  123. $rows = array();
  124. // Wrap each item in an array so that
  125. // it works with the table_theme
  126. foreach($members as $index => $member){
  127. if(is_array($member)){
  128. $rows[] = array('data' => array($index, $member));
  129. foreach($member as $array_index => $array_member_item){
  130. $rows[] = array('data' => array($array_index, $array_member_item));
  131. }
  132. }
  133. else{
  134. $rows[] = array('data' => array($index, $member));
  135. }
  136. }
  137. //Remove the context information which is not needed.
  138. unset($rows[0]);
  139. }
  140. else {
  141. $header = array($items[0]['value']['label']);
  142. $rows = array();
  143. // Wrap each item in an array so that
  144. // it works with the table_theme.
  145. foreach($content as $index => $item){
  146. $rows[] = array('data' => array($index, $item));
  147. }
  148. //Remove the context information which is not needed.
  149. unset($rows[0]);
  150. }
  151. $table = array(
  152. 'header' => $header,
  153. 'rows' => $rows,
  154. 'attributes' => array(
  155. 'id' => 'tripal_table-remote-data-object',
  156. 'class' => 'tripal-data-table'
  157. ),
  158. 'sticky' => FALSE,
  159. 'caption' => "",
  160. 'colgroups' => array(),
  161. 'empty' => 'There is no remote data available.',
  162. );
  163. $content = theme_table($table);
  164. if (count($items) > 0) {
  165. // once we have our table array structure defined, we call Drupal's theme_table()
  166. // function to generate the table.
  167. $element[0] = array(
  168. '#type' => 'markup',
  169. '#markup' => $content,
  170. );
  171. }
  172. }*/
  173. }
  174. /**
  175. * Provides a summary of the formatter settings.
  176. *
  177. * This function corresponds to the hook_field_formatter_settings_summary()
  178. * function of the Drupal Field API.
  179. *
  180. * On the 'Manage Display' page of the content type administration page,
  181. * fields are allowed to provide a settings form. This settings form can
  182. * be used to allow the site admin to define how the field should be
  183. * formatted. The settings are then available for the formatter()
  184. * function of this class. This function provides a text-based description
  185. * of the settings for the site developer to see. It appears on the manage
  186. * display page inline with the field. A field must always return a
  187. * value in this function if the settings form gear button is to appear.
  188. *
  189. * See the hook_field_formatter_settings_summary() function for more
  190. * information.
  191. *
  192. * @param $field
  193. * @param $instance
  194. * @param $view_mode
  195. *
  196. * @return string
  197. * A string that provides a very brief summary of the field settings
  198. * to the user.
  199. *
  200. */
  201. public function settingsSummary($view_mode) {
  202. }
  203. }