remote__data_formatter.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. // Get the site name where the data came from.
  74. $site_id_ws = $this->instance['settings']['data_info']['remote_site'];
  75. $site = db_select('tripal_sites', 'ts')
  76. ->fields('ts', array('name', 'url'))
  77. ->condition('ts.id', $site_id_ws)
  78. ->execute()
  79. ->fetchObject();
  80. $content = '';
  81. if (count($items) > 0) {
  82. $remote_entity_label = $items[0]['remote_entity']['label'];
  83. $remote_entity_page = $items[0]['remote_entity']['ItemPage'];
  84. $content = t('The data below about !label was obtained from the !site database.',
  85. array('!label' => l($remote_entity_label, $remote_entity_page),
  86. '!site' => l($site->name, $site->url)));
  87. }
  88. else {
  89. $content = t('There is no data about this record from the !site database.',
  90. array('!site' => l($site->name, $site->url)));
  91. }
  92. dpm($items);
  93. foreach ($items as $index => $item) {
  94. $value = $item['value'];
  95. if (is_array($value)) {
  96. $headers = array('');
  97. // If this is a collection then handle it as a list of members.
  98. if (array_key_exists('members', $value)) {
  99. foreach ($value['members'] as $subvalue) {
  100. $content .= $this->createTable($subvalue);
  101. }
  102. }
  103. else {
  104. $content .= $this->createTable($subvalue);
  105. }
  106. }
  107. else {
  108. $content .= $this->createDL($value);
  109. }
  110. }
  111. // Return the content for this field.
  112. $element[0] = array(
  113. '#type' => 'markup',
  114. '#markup' => $content,
  115. );
  116. }
  117. /**
  118. * A recursive function for displaying an item in a table.
  119. *
  120. * @param $item
  121. * An item from the $items array passed to the view() function.
  122. * @return
  123. * An HTML formatted Table.
  124. */
  125. private function createTable($item, &$pkey = '', &$rows = array(), $depth = 0) {
  126. foreach ($item as $key => $value) {
  127. // Skip JSON-LD keys.
  128. if (preg_match('/^\@/', $key)) {
  129. continue;
  130. }
  131. $key = preg_replace('/_/', ' ', $key);
  132. $key = ucwords($key);
  133. if ($pkey) {
  134. $key = $pkey . ' ' . $key;
  135. }
  136. if (is_array($value)) {
  137. $this->createTable($value, $key, $rows, $depth + 1);
  138. }
  139. else {
  140. $rows[] = array(
  141. 'data'=> array(
  142. $key,
  143. $value
  144. ),
  145. 'no_striping' => TRUE,
  146. );
  147. }
  148. }
  149. if ($depth == 0) {
  150. $headers = array('Data Type', 'Value');
  151. return theme_table(array(
  152. 'header' => $headers,
  153. 'rows' => $rows,
  154. 'attributes' => array(
  155. 'class' => 'tripal-remote--data-field-table',
  156. ),
  157. 'sticky' => FALSE,
  158. 'caption' => "",
  159. 'colgroups' => array(),
  160. 'empty' => 'There are no results.',
  161. ));
  162. }
  163. }
  164. /**
  165. * A recursive function for creating an HTML dictionary list from
  166. * the results for the item provided.
  167. *
  168. * @param $item
  169. * An item from the $items array passed to the view() function.
  170. * @return
  171. * An HTML formatted DL.
  172. */
  173. private function createDL($item, &$pkey = '', &$content= '', $depth = 0) {
  174. if ($depth == 0) {
  175. $content = '<dl class="tripal-remote--data-field-dl">';
  176. }
  177. foreach ($item as $key => $value) {
  178. // Skip JSON-LD keys.
  179. if (preg_match('/^\@/', $key)) {
  180. continue;
  181. }
  182. $key = preg_replace('/_/', ' ', $key);
  183. $key = ucwords($key);
  184. if ($pkey) {
  185. $key = $pkey . ' ' . $key;
  186. }
  187. if (is_array($value)) {
  188. $this->createDL($value, $key, $content, $depth + 1);
  189. }
  190. else {
  191. $content .= '<dt>' . $key . '&nbsp;:&nbsp;</dt><dd>' . $value . '</dd>';
  192. }
  193. }
  194. if ($depth == 0) {
  195. $content .= '</dl>';
  196. return $content;
  197. }
  198. }
  199. /**
  200. * A recursive function for creating an HTML dictionary list from
  201. * the results for the item provided.
  202. *
  203. * @param $item
  204. * An item from the $items array passed to the view() function.
  205. * @return
  206. * An HTML formatted DL.
  207. */
  208. private function createNestedDL($item) {
  209. $content = '<dl>';
  210. foreach ($item as $key => $value) {
  211. // Skip JSON-LD keys.
  212. if (preg_match('/^\@/', $key)) {
  213. continue;
  214. }
  215. $key = preg_replace('/_/', ' ', $key);
  216. $key = ucwords($key);
  217. if (is_array($value)) {
  218. $value = $this->createDL($value);
  219. }
  220. $content .= '<dt>' . $key . '</dt><dd>' . $value . '</dd>';
  221. }
  222. $content .= '</dl>';
  223. return $content;
  224. }
  225. /**
  226. * Provides a summary of the formatter settings.
  227. *
  228. * This function corresponds to the hook_field_formatter_settings_summary()
  229. * function of the Drupal Field API.
  230. *
  231. * On the 'Manage Display' page of the content type administration page,
  232. * fields are allowed to provide a settings form. This settings form can
  233. * be used to allow the site admin to define how the field should be
  234. * formatted. The settings are then available for the formatter()
  235. * function of this class. This function provides a text-based description
  236. * of the settings for the site developer to see. It appears on the manage
  237. * display page inline with the field. A field must always return a
  238. * value in this function if the settings form gear button is to appear.
  239. *
  240. * See the hook_field_formatter_settings_summary() function for more
  241. * information.
  242. *
  243. * @param $field
  244. * @param $instance
  245. * @param $view_mode
  246. *
  247. * @return string
  248. * A string that provides a very brief summary of the field settings
  249. * to the user.
  250. *
  251. */
  252. public function settingsSummary($view_mode) {
  253. }
  254. }