remote__data_formatter.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. //Check that the load function returned content.
  74. if (!empty($items[0]['value'])){
  75. $list = array();
  76. foreach ($items as $index => $item) {
  77. $list[$index] = $item['value'];
  78. }
  79. // If more than one value has been found display all values in an unordered
  80. // list.
  81. if (count($list) > 1) {
  82. $content = theme_item_list(array(
  83. 'items' => $list,
  84. 'title' => '',
  85. 'attributes' => array('class' => array($entity->bundle . '-remote-data-list', 'remote-data-field-list')),
  86. 'type' => 'ul'
  87. ));
  88. }
  89. else {
  90. $content = $list[0];
  91. }
  92. if(array_key_exists('members', $content)){
  93. $header = array($items[0]['value']['label']);
  94. $members = $content['members'];
  95. $rows = array();
  96. // Wrap each item in an array incase it is an array so that
  97. // it works with the table_theme
  98. foreach($members as $index => $member){
  99. $rows[] = array('data' => array($index, $item));
  100. }
  101. //Remove the context information which is not needed.
  102. unset($rows[0]);
  103. }
  104. else {
  105. $header = array($items[0]['value']['label']);
  106. $rows = array();
  107. // Wrap each item in an array incase it is an array so that
  108. // it works with the table_theme.
  109. foreach($content as $index => $item){
  110. $rows[] = array('data' => array($index, $item));
  111. }
  112. //Remove the context information which is not needed.
  113. unset($rows[0]);
  114. }
  115. $table = array(
  116. 'header' => $header,
  117. 'rows' => $rows,
  118. 'attributes' => array(
  119. 'id' => 'tripal_table-remote-data-object',
  120. 'class' => 'tripal-data-table'
  121. ),
  122. 'sticky' => FALSE,
  123. 'caption' => "",
  124. 'colgroups' => array(),
  125. 'empty' => 'There is no remote data available.',
  126. );
  127. $content = theme_table($table);
  128. if (count($items) > 0) {
  129. // once we have our table array structure defined, we call Drupal's theme_table()
  130. // function to generate the table.
  131. $element[0] = array(
  132. '#type' => 'markup',
  133. '#markup' => $content,
  134. );
  135. }
  136. }
  137. }
  138. /**
  139. * Provides a summary of the formatter settings.
  140. *
  141. * This function corresponds to the hook_field_formatter_settings_summary()
  142. * function of the Drupal Field API.
  143. *
  144. * On the 'Manage Display' page of the content type administration page,
  145. * fields are allowed to provide a settings form. This settings form can
  146. * be used to allow the site admin to define how the field should be
  147. * formatted. The settings are then available for the formatter()
  148. * function of this class. This function provides a text-based description
  149. * of the settings for the site developer to see. It appears on the manage
  150. * display page inline with the field. A field must always return a
  151. * value in this function if the settings form gear button is to appear.
  152. *
  153. * See the hook_field_formatter_settings_summary() function for more
  154. * information.
  155. *
  156. * @param $field
  157. * @param $instance
  158. * @param $view_mode
  159. *
  160. * @return string
  161. * A string that provides a very brief summary of the field settings
  162. * to the user.
  163. *
  164. */
  165. public function settingsSummary($view_mode) {
  166. }
  167. }