so__transcript_formatter.inc 8.8 KB

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