sio__references_formatter.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. class sio__references_formatter extends ChadoFieldFormatter {
  3. // The default label for this field.
  4. public static $default_label = 'References';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['sio__references'];
  7. /**
  8. *
  9. * @see TripalFieldFormatter::view()
  10. */
  11. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  12. $field_name = $this->field['field_name'];
  13. $chado_table = $this->instance['settings']['chado_table'];
  14. // Do we have an empty list? If so, just return.
  15. if (!$items[0]['value']) {
  16. return;
  17. }
  18. // First, organize the values by their types.
  19. $ordered_items = [];
  20. foreach ($items as $delta => $item) {
  21. $type = isset($item['value']['rdfs:type']) ? $item['value']['rdfs:type'] : '';
  22. $ientity = isset($item['value']['entity']) ? $item['value']['entity'] : '';
  23. $name = isset($item['value']['schema:name']) ? $item['value']['schema:name'] : '';
  24. $identifier = isset($item['value']['data:0842']) ? $item['value']['data:0842'] : '';
  25. if ($ientity) {
  26. list($entity_type, $entity_id) = explode(':', $ientity);
  27. $name = l(strip_tags($name), 'bio_data/' . $entity_id);
  28. }
  29. $ordered_items[ucfirst($type)][] = $name;
  30. }
  31. // Reorder the list so it's compatible with theming a list.
  32. ksort($ordered_items);
  33. // Generate the pagers for each type.
  34. $list_items = [];
  35. $headers = [];
  36. $rows = [];
  37. foreach ($ordered_items as $type => $children) {
  38. $items_per_page = array_key_exists('items_per_page', $this->instance['settings']) ? $this->instance['settings']['items_per_page'] : 10;
  39. $total_records = count($children);
  40. $total_pages = (int) ($total_records / $items_per_page) + 1;
  41. $pelement = 0;
  42. $current_page = pager_default_initialize($total_records, $items_per_page, $pelement);
  43. $pager = theme('pager', [
  44. 'tags' => [],
  45. 'element' => $pelement,
  46. 'parameters' => [],
  47. 'quantity' => 5,
  48. ]);
  49. $pager = $this->ajaxifyPager($pager, $entity);
  50. $page_items = array_chunk($children, $items_per_page);
  51. $rows[] = [
  52. [
  53. 'data' => ucfirst($type) . '(s)',
  54. 'header' => TRUE,
  55. 'width' => '20%',
  56. ],
  57. theme_item_list([
  58. 'items' => $page_items[$current_page],
  59. 'title' => '',
  60. 'type' => 'ul',
  61. 'attributes' => [],
  62. ]) . $pager,
  63. ];
  64. }
  65. $table = [
  66. 'header' => [],
  67. 'rows' => $rows,
  68. 'attributes' => [
  69. 'id' => 'sio__references-table',
  70. 'class' => 'tripal-data-table',
  71. ],
  72. 'sticky' => FALSE,
  73. 'caption' => "",
  74. 'colgroups' => [],
  75. 'empty' => 'There are no records in this site to which this publication refers.',
  76. ];
  77. $content = theme_table($table);
  78. $element[0] = [
  79. '#type' => 'markup',
  80. '#markup' => $content,
  81. ];
  82. }
  83. }