sbo__relationship_formatter.inc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. class sbo__relationship_formatter extends ChadoFieldFormatter {
  3. // The default lable for this field.
  4. public static $default_label = 'Relationship';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('sbo__relationship');
  7. /**
  8. *
  9. * @see TripalFieldFormatter::settingsForm()
  10. */
  11. public function settingsForm($view_mode, $form, &$form_state) {
  12. }
  13. /**
  14. *
  15. * @see TripalFieldFormatter::view()
  16. */
  17. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  18. // Get the settings
  19. $settings = $display['settings'];
  20. $rows = array();
  21. $headers = array('Subject' ,'Type', 'Object');
  22. $headers = array('Relationship');
  23. foreach ($items as $delta => $item) {
  24. if (!$item['value']) {
  25. continue;
  26. }
  27. $subject_name = $item['value']['local:relationship_subject']['schema:name'];
  28. $subject_type = $item['value']['local:relationship_subject']['rdfs:type'];
  29. $object_name = $item['value']['local:relationship_object']['schema:name'];
  30. $object_type = $item['value']['local:relationship_object']['rdfs:type'];
  31. $phrase = $item['value']['SIO:000493'];
  32. // Handle some special cases.
  33. // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
  34. // we want to show the parent gene and the protein.
  35. if ($object_type == 'mRNA' and ($subject_type != 'polypeptide')) {
  36. continue;
  37. }
  38. if ($subject_type == 'mRNA' and ($object_type != 'gene')) {
  39. continue;
  40. }
  41. $phrase = preg_replace("/$subject_type/", "<b>$subject_type</b>", $phrase);
  42. $phrase = preg_replace("/$object_type/", "<b>$object_type</b>", $phrase);
  43. if (array_key_exists('entity', $item['value']['object'])) {
  44. list($entity_type, $object_entity_id) = explode(':', $item['value']['object']['entity']);
  45. if ($object_entity_id != $entity->id) {
  46. $link = l($object_name, 'bio_data/' . $object_entity_id);
  47. $phrase = preg_replace("/$object_name/", $link, $phrase);
  48. }
  49. }
  50. if (array_key_exists('entity', $item['value']['subject'])) {
  51. list($entity_type, $subject_entity_id) = explode(':', $item['value']['subject']['entity']);
  52. if ($subject_entity_id != $entity->id) {
  53. $link = l($subject_name, 'bio_data/' . $subject_entity_id);
  54. $phrase = preg_replace("/$subject_name/", $link, $phrase);
  55. }
  56. }
  57. $rows[] = array($phrase);
  58. }
  59. // the $table array contains the headers and rows array as well as other
  60. // options for controlling the display of the table. Additional
  61. // documentation can be found here:
  62. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  63. $table = array(
  64. 'header' => $headers,
  65. 'rows' => $rows,
  66. 'attributes' => array(
  67. 'id' => 'chado-linker--relationship-table',
  68. 'class' => 'tripal-data-table'
  69. ),
  70. 'sticky' => FALSE,
  71. 'caption' => '',
  72. 'colgroups' => array(),
  73. 'empty' => 'There are no relationships',
  74. );
  75. // once we have our table array structure defined, we call Drupal's theme_table()
  76. // function to generate the table.
  77. if (count($items) > 0) {
  78. $element[0] = array(
  79. '#type' => 'markup',
  80. '#markup' => theme_table($table),
  81. );
  82. }
  83. }
  84. }