sio__annotation_formatter.inc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. class sio__annotation_formatter extends ChadoFieldFormatter {
  3. // The default lable for this field.
  4. public static $default_label = 'Chado Annotation';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = ['chado_linker__cvterm'];
  7. /**
  8. *
  9. * @see TripalFieldFormatter::view()
  10. */
  11. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  12. $headers = ['Term', 'Name', 'Definition'];
  13. $rows = [];
  14. $field_table = $this->instance['settings']['chado_table'];
  15. $schema = chado_get_schema($field_table);
  16. $vocabulary_term = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  17. $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
  18. $definition_term = tripal_get_chado_semweb_term('cvterm', 'definition');
  19. $name_term = tripal_get_chado_semweb_term('cvterm', 'name');
  20. if (array_key_exists('is_not', $schema['fields'])) {
  21. $negation_term = chado_get_semweb_term($field_table, 'is_not');
  22. }
  23. $chado_table = $this->instance['settings']['chado_table'];
  24. foreach ($items as $delta => $item) {
  25. if (!empty($item['chado-' . $chado_table . '__cvterm_id'])) {
  26. $cvterm = chado_generate_var('cvterm', ['cvterm_id' => $item['chado-' . $chado_table . '__cvterm_id']]);
  27. $dbxref = $cvterm->dbxref_id;
  28. // Build the accession.
  29. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  30. if ($dbxref->db_id->urlprefix) {
  31. $accession = l($accession, chado_get_dbxref_url($dbxref), ['attributes' => ['target' => '_blank']]);
  32. }
  33. $row = [
  34. $accession,
  35. $item['value'][$name_term],
  36. $item['value'][$definition_term],
  37. ];
  38. if (array_key_exists('is_not', $schema['fields'])) {
  39. if ($negation_term == FALSE) {
  40. $row[1] = 'NOT ' . $row[1];
  41. }
  42. }
  43. $rows[] = $row;
  44. }
  45. }
  46. // Theme the results in a talbe.
  47. $caption = 'This record has the following annotations.';
  48. $table = [
  49. 'header' => $headers,
  50. 'rows' => $rows,
  51. 'attributes' => [
  52. 'id' => "$chado_table-table-terms",
  53. 'class' => 'tripal-data-table',
  54. ],
  55. 'caption' => $caption,
  56. 'sticky' => FALSE,
  57. 'colgroups' => [],
  58. 'empty' => 'There are no annotations of this type',
  59. ];
  60. if (count($items) > 0) {
  61. $element[0] = [
  62. '#type' => 'markup',
  63. '#markup' => theme_table($table),
  64. ];
  65. }
  66. }
  67. }