tripal_example.theme.inc

This file should contain all Drupal hooks for theming content. For templates that need specific the hook_preprocess functions should be included here

File

tripal_example/theme/tripal_example.theme.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This file should contain all Drupal hooks for theming content. For templates
  6. * that need specific the hook_preprocess functions should be included here
  7. *
  8. */
  9. /**
  10. * implementation of hook_preprocess_HOOK()
  11. *
  12. * Used to alter or add to theme variables. The variables are passed into
  13. * templates when processing. This function organizes the relationships
  14. * into more simple structures for parsing in the template file.
  15. *
  16. * @ingroup tripal_example
  17. */
  18. function tripal_example_preprocess_tripal_example_relationships(&$variables) {
  19. // EXPLANATION: If you have implemented a new Chado node type and the record
  20. // that belongs to the node has a corresponding xxxx_relationship table
  21. // this this function can be used to provide relationships to the template
  22. // in a format that is easier to parse. This is one example where specific SQL
  23. // statements can improve performance over Tripal API calls. SQL is not
  24. // recommended inside of template files, but rather the Tripal API calls only.
  25. // Therefore, this function queries the relationships and then organizes them
  26. // into arrays that are easier and faster to parse. You should be able to
  27. // copy the content of this function and adjust as necessary to change table
  28. // names if your record has relationships.
  29. $example = $variables['node']->example;
  30. // expand the example object to include the example relationships.
  31. $options = array(
  32. 'return_array' => 1,
  33. // we don't want to fully recurs we only need information about the
  34. // relationship type and the object and subject examples (including example
  35. // type)
  36. 'include_fk' => array(
  37. 'type_id' => 1,
  38. 'object_id' => array(
  39. 'type_id' => 1,
  40. ),
  41. 'subject_id' => array(
  42. 'type_id' => 1,
  43. ),
  44. ),
  45. );
  46. $example = chado_expand_var($example, 'table', 'example_relationship', $options);
  47. // get the subject relationships
  48. $srelationships = $example->example_relationship->subject_id;
  49. $orelationships = $example->example_relationship->object_id;
  50. // combine both object and subject relationships into a single array
  51. $relationships = array();
  52. $relationships['object'] = array();
  53. $relationships['subject'] = array();
  54. // iterate through the object relationships
  55. if ($orelationships) {
  56. foreach ($orelationships as $relationship) {
  57. $rel = new stdClass();
  58. $rel->record = $relationship;
  59. // get the relationship and child types
  60. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  61. $child_type = $relationship->subject_id->type_id->name;
  62. // get the node id of the subject
  63. $sql = "SELECT nid FROM {chado_example} WHERE example_id = :example_id";
  64. $n = db_query($sql, array(':example_id' => $relationship->subject_id->example_id))->fetchObject();
  65. if ($n) {
  66. $rel->record->nid = $n->nid;
  67. }
  68. if (!array_key_exists($rel_type, $relationships['object'])) {
  69. $relationships['object'][$rel_type] = array();
  70. }
  71. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  72. $relationships['object'][$rel_type][$child_type] = array();
  73. }
  74. $relationships['object'][$rel_type][$child_type][] = $rel;
  75. }
  76. }
  77. // now add in the subject relationships
  78. if ($srelationships) {
  79. foreach ($srelationships as $relationship) {
  80. $rel = new stdClass();
  81. $rel->record = $relationship;
  82. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  83. $parent_type = $relationship->object_id->type_id->name;
  84. // get the node id of the subject
  85. $sql = "SELECT nid FROM {chado_example} WHERE example_id = :example_id";
  86. $n = db_query($sql, array(':example_id' => $relationship->object_id->example_id))->fetchObject();
  87. if ($n) {
  88. $rel->record->nid = $n->nid;
  89. }
  90. if (!array_key_exists($rel_type, $relationships['subject'])) {
  91. $relationships['subject'][$rel_type] = array();
  92. }
  93. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  94. $relationships['subject'][$rel_type][$parent_type] = array();
  95. }
  96. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  97. }
  98. }
  99. $example->all_relationships = $relationships;
  100. }