so__transcript_formatter.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // Get the settings
  13. $settings = $display['settings'];
  14. $_entity = $entity;
  15. // if no transcript (mRNA) fields are selected in the transcript fields edit page, use a table to list the mRNA name, id, type and location only
  16. $headers = ['Transcript Name', 'Identifier', 'Type', 'Location'];
  17. $rows = [];
  18. $mRNA_fieldset = [];
  19. $fields_exist = FALSE;
  20. foreach ($items as $delta => $item) {
  21. if (!$item['value']) {
  22. continue;
  23. }
  24. $transcript = $item['value'];
  25. // Get the field values
  26. $feature_name = $transcript['schema:name'];
  27. $feature_uname = $transcript['data:0842'];
  28. $loc = $transcript['SO:0000735'];
  29. $type = $transcript['rdfs:type'];
  30. // Add a link if there is an entity.
  31. if (array_key_exists('entity', $item['value']) and $item['value']['entity']) {
  32. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  33. $transcript_name = $feature_name;
  34. // the transcript id is the identifier for the mRNA transcript
  35. $transcript_id = $entity_id;
  36. $mRNA_fieldset[$transcript_id] = [
  37. '#type' => 'fieldset',
  38. '#title' => $transcript_name,
  39. '#description' => '',
  40. '#collapsible' => TRUE,
  41. '#collapsed' => TRUE,
  42. '#attributes' => ['class' => ['collapsible', 'collapsed']],
  43. ];
  44. drupal_add_library('system', 'drupal.collapse');
  45. $mRNA_fieldset[$transcript_id]['table'] = [];
  46. $transcript_fields_header = [];
  47. $transcript_field_rows = [];
  48. $single_cardinality_fields_exist = FALSE;
  49. // mRNA expanded fields
  50. // obtain each field for the mRNA as specified in the transcripts field edit configuration
  51. $fields = $this->instance['settings']['transcript_fields'];
  52. $entity = TripalEntity_load($entity_id);
  53. foreach ($fields as $field_name => $field_value) {
  54. if (array_key_exists('show', $field_value) and ($field_value['show'] != 1)) {
  55. // this field is not selected for display in the transcripts field edit configuration page
  56. continue;
  57. }
  58. $fields_exist = TRUE;
  59. // Load the field info.
  60. $field_info = field_info_field($field_name);
  61. $field_instance = field_info_instance('TripalEntity', $field_name, $entity->bundle); // get title and description
  62. $field_items = field_get_items('TripalEntity', $entity, $field_name);
  63. $field_element = field_view_field('TripalEntity', $entity, $field_name, $field_instance['display']['default']);
  64. $field_element['#label_display'] = 'hidden';
  65. // use $_entity parameter for gene to obtain the 'hide empty' gene field setting
  66. $bundle = tripal_load_bundle_entity(['name'=> $_entity->bundle]);
  67. $hide_empty = tripal_get_bundle_variable('hide_empty_field', $bundle->id);
  68. $cardinality = array_key_exists('cardinality', $field_info) ? $field_info['cardinality'] : 1;
  69. $field_is_empty = $this->isFieldEmpty($field_items);
  70. // do not show this field if empty and specified to be hidden from the gene edit page
  71. if ($field_is_empty and $hide_empty) {
  72. continue;
  73. }
  74. // check field cardinality
  75. if ($cardinality == 1) {
  76. // add field to a special transcripts table, where fields have cardinality of 1
  77. $single_cardinality_fields_exist = TRUE;
  78. $transcript_field_rows[] = [
  79. [
  80. 'data' => $field_instance['label'],
  81. 'header' => TRUE,
  82. ],
  83. [
  84. 'data' => drupal_render($field_element),
  85. ]
  86. ];
  87. }
  88. else {
  89. // render the fields with cardinality not 1 individually after the special transcripts table.
  90. $mRNA_fieldset[$transcript_id][$field_name] = [
  91. '#type' => 'item',
  92. '#title' => $field_instance['label'],
  93. '#description' => $field_instance['description'],
  94. '#markup' => drupal_render($field_element),
  95. ];
  96. }
  97. }
  98. $feature_name = l($feature_name, "bio_data/" . $entity_id, ['attributes' => ['target' => "_blank"]]);
  99. if ($single_cardinality_fields_exist) {
  100. // display fields of single cardinality in a special transcripts table
  101. $transcript_fields_table = [
  102. 'header' => $transcript_fields_header,
  103. 'rows' => $transcript_field_rows,
  104. 'attributes' => [
  105. 'id' => 'tripal_feature-table-transcript-fields-object',
  106. 'class' => 'tripal-data-table',
  107. ],
  108. 'sticky' => FALSE,
  109. 'caption' => "",
  110. 'colgroups' => [],
  111. 'empty' => 'This feature has no single cardinality transcript fields',
  112. ];
  113. $mRNA_fieldset[$transcript_id]['table'] = [
  114. '#type' => 'markup',
  115. '#markup' => theme_table($transcript_fields_table),
  116. ];
  117. }
  118. }
  119. $mRNA_fieldset[$transcript_id]['mRNA_link'] = [
  120. '#type' => 'markup',
  121. '#markup' => "For further details go to ".$feature_name,
  122. ];
  123. $rows[] = [$feature_name, $feature_uname, $type, $loc];
  124. }
  125. $table = [
  126. 'header' => $headers,
  127. 'rows' => $rows,
  128. 'attributes' => [
  129. 'id' => 'tripal_feature-table-transcripts-object',
  130. 'class' => 'tripal-data-table',
  131. ],
  132. 'sticky' => FALSE,
  133. 'caption' => "",
  134. 'colgroups' => [],
  135. 'empty' => 'This feature has no transcripts',
  136. ];
  137. // once we have our table array structure defined, we call Drupal's theme_table()
  138. // function to generate the table.
  139. if (count($items) > 0) {
  140. if ($fields_exist) {
  141. $content = $mRNA_fieldset;
  142. $element[0] = $content;
  143. }
  144. else {
  145. $content = theme_table($table);
  146. $element[0] = [
  147. '#type' => 'markup',
  148. '#markup' => $content,
  149. ];
  150. }
  151. }
  152. }
  153. /**
  154. * Checks if the field is empty.
  155. *
  156. * @param $field_items
  157. * The TripalEntity field items, holds the value for the field.
  158. *
  159. * @return bool
  160. * TRUE if field is empty, otherwise FALSE.
  161. */
  162. public function isFieldEmpty($field_items) {
  163. $field_is_empty = FALSE;
  164. // handle when field_items is an array with single of multiple values
  165. if (is_array($field_items)) {
  166. if (sizeof($field_items) == 1) {
  167. if (sizeof($field_items[0]) == 1) {
  168. if (is_string($field_items[0]['value'])) {
  169. if (strlen($field_items[0]['value']) == 0) {
  170. // the value is of zero length
  171. $field_is_empty = TRUE;
  172. }
  173. }
  174. }
  175. else {
  176. $all_empty = TRUE;
  177. foreach ($field_items[0] as $key=>$val) {
  178. if ((is_string($val) and (strlen($val) != 0)) or (!is_string($val))) {
  179. // the value is a string of non-zero length, or not a string at all
  180. $all_empty = FALSE;
  181. }
  182. }
  183. if ($all_empty) {
  184. $field_is_empty = TRUE;
  185. }
  186. }
  187. }
  188. }
  189. elseif ($field_items == null) {
  190. $field_is_empty = TRUE;
  191. }
  192. return $field_is_empty;
  193. }
  194. }