so__transcript_formatter.inc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. class so__transcript_formatter extends ChadoFieldFormatter {
  3. // The default label for this field.
  4. public static $default_label = 'Transcript';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['so__transcript'];
  7. /**
  8. *
  9. * @see TripalFieldFormatter::view()
  10. */
  11. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  12. $mRNA_fieldset = [];
  13. // If there are no items return an empty element.
  14. if (count($items) == 0) {
  15. return $this->returnEmpty($element);
  16. }
  17. // Iterate through the list of fields so that we can load the mRNA
  18. // entity with all of the field requested by the site admin. If we
  19. // don't do this first then auto_attach fields won't be added.
  20. $fields = $this->instance['settings']['transcript_fields'];
  21. $field_ids = [];
  22. foreach ($fields as $field_name => $field_value) {
  23. $field_info = field_info_field($field_name);
  24. $field_ids[] = $field_info['id'];
  25. }
  26. // For backwards compatibility if no field IDs were provided then
  27. // return the original table of four rows.
  28. if (count($field_ids) == 0) {
  29. return $this->returnDefaulTable($element, $items);
  30. }
  31. // Iterate through each mRNA (transcript).
  32. $transcripts = [];
  33. foreach ($items as $delta => $item) {
  34. // Skip empty items;
  35. if (!$item['value']) {
  36. continue;
  37. }
  38. // If any transcript is not published (i.e. it doesn't have an entity)
  39. // then we want to just return the default table and stop.
  40. if (!array_key_exists('entity', $item['value'])) {
  41. return $this->returnDefaulTable($element, $items);
  42. }
  43. list($entity_type, $mRNA_entity_id) = explode(':', $item['value']['entity']);
  44. // Now load the mRNA entity with all of the fields.
  45. $result = tripal_load_entity('TripalEntity', [$mRNA_entity_id], FALSE, $field_ids);
  46. reset($result);
  47. $mRNA_entity = $result[$mRNA_entity_id];
  48. // Create the fieldset for this transcript.
  49. $transcripts[$mRNA_entity_id] = [
  50. '#type' => 'fieldset',
  51. '#title' => $mRNA_entity->title,
  52. '#description' => '',
  53. '#collapsible' => TRUE,
  54. '#collapsed' => TRUE,
  55. '#attributes' => ['class' => ['collapsible', 'collapsed']],
  56. ];
  57. drupal_add_library('system', 'drupal.collapse');
  58. // Add a link to the mRNA page.
  59. $feature_name = $mRNA_entity->title;
  60. $feature_name = l($feature_name, "bio_data/" . $mRNA_entity->id, ['attributes' => ['target' => "_blank"]]);
  61. $transcripts[$mRNA_entity_id]['mRNA_link'] = [
  62. '#type' => 'markup',
  63. '#markup' => "<i>Click, " . $feature_name . ", For the full transcript page.</i>",
  64. ];
  65. // Now add all fields to the fieldset.
  66. $this->addFieldsetFields($mRNA_entity, $fields, $transcripts[$mRNA_entity_id]);
  67. }
  68. $element[0] = $transcripts;
  69. }
  70. /**
  71. * Builds a fieldset for the entity.
  72. */
  73. private function addFieldsetFields($mRNA_entity, $fields, &$fieldset) {
  74. // Get some settings about the transcript content type.
  75. $mRNA_bundle = tripal_load_bundle_entity(['name'=> $mRNA_entity->bundle]);
  76. $hide_empty = tripal_get_bundle_variable('hide_empty_field', $mRNA_bundle->id);
  77. // Get the Entity ID for the transcript.
  78. $entity_id = $mRNA_entity->id;
  79. // We will use a summary table to house the fields that have a
  80. // cardinality of one. These will appear at the top of the fieldset.
  81. $summary_rows = [];
  82. // Iterate through the list of fields that the site admin has indicated
  83. // that they want to show in the transcript fieldset. They are provided
  84. // in order that they should be shown.
  85. foreach ($fields as $field_name => $field_value) {
  86. // Skip fields that the site admin does not want to show.
  87. if (array_key_exists('show', $field_value) and ($field_value['show'] != 1)) {
  88. continue;
  89. }
  90. // Load the field instance info. We'll need this to determine the
  91. // cardinality of the field and to render it for display.
  92. $field_info = field_info_field($field_name);
  93. $field_instance = field_info_instance('TripalEntity', $field_name, $mRNA_entity->bundle);
  94. // For the display we want to honor the site admin's wishes and not
  95. // show fields that are empty if they have that setting turned on.
  96. $field_items = field_get_items('TripalEntity', $mRNA_entity, $field_name);
  97. $field_is_empty = tripal_field_is_empty($field_info, $field_items);
  98. if ($field_is_empty and $hide_empty) {
  99. continue;
  100. }
  101. // If the default display is to hide this field then skip it too.
  102. if ($field_instance['display']['default']['type'] == 'hidden') {
  103. continue;
  104. }
  105. // Get the render array for this field.
  106. $field_element = field_view_field('TripalEntity', $mRNA_entity, $field_name, $field_instance['display']['default']);
  107. $field_element['#label_display'] = 'hidden';
  108. // We need to know the cardinality of this field. If it has a
  109. // cardinatliy of 1 we'll put it in the summary table for the
  110. // transcript that appears at the top of the fieldset. If not we'll
  111. // let it render as is, in the order it's provided to us here.
  112. $cardinality = array_key_exists('cardinality', $field_info) ? $field_info['cardinality'] : 1;
  113. if ($cardinality == 1) {
  114. // add field to a special transcripts table, where fields have cardinality of 1
  115. $summary_rows[] = [
  116. [
  117. 'data' => $field_instance['label'],
  118. 'header' => TRUE,
  119. ],
  120. [
  121. 'data' => drupal_render($field_element),
  122. ]
  123. ];
  124. }
  125. // Else add the field as is.
  126. else {
  127. $fieldset[$field_name . '_header'] = [
  128. '#type' => 'markup',
  129. '#markup' => '<h3 class="tripal-chado-gene-transcript-fieldset-item">' . $field_instance['label'] . '</h3>',
  130. ];
  131. $fieldset[$field_name] = [
  132. '#type' => 'markup',
  133. '#markup' => drupal_render($field_element),
  134. ];
  135. }
  136. } // End looping over fields.
  137. // If the summary table has values then
  138. if (count($summary_rows) > 0) {
  139. // display fields of single cardinality in a special transcripts table
  140. $summary_table = [
  141. 'header' => [],
  142. 'rows' => $summary_rows,
  143. 'attributes' => [
  144. 'id' => 'tripal_feature-table-transcript-fields-object',
  145. 'class' => 'tripal-data-table',
  146. ],
  147. 'sticky' => FALSE,
  148. 'caption' => "",
  149. 'colgroups' => [],
  150. 'empty' => 'This feature has no single cardinality transcript fields',
  151. ];
  152. $fieldset['summary_header'] = [
  153. '#type' => 'markup',
  154. '#markup' => '<h3>Transcript Summary</h3>',
  155. '#weight' => -101,
  156. ];
  157. $fieldset['summary_table'] = [
  158. '#type' => 'markup',
  159. '#markup' => theme_table($summary_table),
  160. '#weight' => -100,
  161. ];
  162. }
  163. }
  164. /**
  165. * Returns the default table.
  166. *
  167. * For backwards compatibility this function returns a table
  168. * of four columns and one row per transcript.
  169. */
  170. private function returnDefaulTable(&$element, $items) {
  171. $default_headers = ['Transcript Name', 'Identifier', 'Type', 'Location'];
  172. $default_rows = [];
  173. foreach ($items as $delta => $item) {
  174. if (!$item['value']) {
  175. continue;
  176. }
  177. // Get the field values.
  178. $feature_name = $item['value']['schema:name'];
  179. $feature_uname = $item['value']['data:0842'];
  180. $loc = $item['value']['SO:0000735'];
  181. $type = $item['value']['rdfs:type'];
  182. $feature_name = l($feature_name, "bio_data/" . $entity_id, ['attributes' => ['target' => "_blank"]]);
  183. $default_rows[] = [$feature_name, $feature_uname, $type, $loc];
  184. }
  185. // Build the default table
  186. $default_table = [
  187. 'header' => $default_headers,
  188. 'rows' => $default_rows,
  189. 'attributes' => [
  190. 'id' => 'tripal_feature-table-transcripts-object',
  191. 'class' => 'tripal-data-table',
  192. ],
  193. 'sticky' => FALSE,
  194. 'caption' => "",
  195. 'colgroups' => [],
  196. 'empty' => 'This feature has no transcripts',
  197. ];
  198. $element[0] = [
  199. '#type' => 'markup',
  200. '#markup' => theme_table($default_table),
  201. ];
  202. }
  203. /**
  204. * Returns an empty element for the view.
  205. */
  206. private function returnEmpty(&$element){
  207. $element[0] = [
  208. '#type' => 'markup',
  209. '#markup' => '',
  210. ];
  211. }
  212. }