sbo__relationship_formatter.inc 3.3 KB

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