views_handler_field_node_optional.inc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @file
  4. * Field handler to provide simple renderer that allows linking to a node.
  5. *
  6. * @ingroup views_field_handlers
  7. * @ingroup tripal_core
  8. */
  9. class views_handler_field_node_optional extends views_handler_field_node {
  10. function construct() {
  11. parent::construct();
  12. $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
  13. if (module_exists('translation')) {
  14. $this->additional_fields['language'] = array('table' => 'node', 'field' => 'language');
  15. }
  16. }
  17. /**
  18. * Add chado_* and *_node alias'd left joins to the table
  19. */
  20. function query() {
  21. // Check what we have (ie: current table? node table? )
  22. $chado_table = 'chado_' . $this->table;
  23. foreach ($this->query->table_queue as $table_def) {
  24. //check is $this->table
  25. if ($table_def['table'] == $this->table) {
  26. $this_table_alias = $table_def['alias'];
  27. }
  28. // check is node joined to #this->table
  29. if ($table_def['table'] == $chado_table) {
  30. $node_table_alias = $table_def['alias'];
  31. }
  32. }
  33. // First: Add the main field-----------------------------------------------
  34. if (!$this_table_alias) {
  35. $this_table_alias = $this->ensure_my_table();
  36. }
  37. $field_alias = $this->query->add_field($this_table_alias, $this->real_field);
  38. $this->aliases[ $this->real_field ] = $field_alias;
  39. $this->field_alias = $field_alias;
  40. // Second: Add nid field and Joins if necessary----------------------------
  41. // Add node join if needed
  42. if (!$node_table_alias) {
  43. $def['table'] = $chado_table;
  44. $def['field'] = $this->table . '_id';
  45. $def['left_table'] = $this_table_alias;
  46. $def['left_field'] = $this->table . '_id';
  47. $join = new views_join();
  48. $join->definition = $def;
  49. $join->construct();
  50. $join->adjusted = TRUE;
  51. $node_table_alias = $this->query->add_relationship($def['table'], $join, $def['table']);
  52. }
  53. // Finally Add Field
  54. $field_alias = $this->query->add_field($node_table_alias, 'nid');
  55. $this->aliases['nid'] = $field_alias;
  56. }
  57. /**
  58. * Render whatever the data is as a link to the node.
  59. *
  60. * Data should be made XSS safe prior to calling this function.
  61. */
  62. function render_link($data, $values) {
  63. if (!empty($this->options['link_to_node']) && $data !== NULL && $data !== '') {
  64. if (!empty($values->{$this->aliases['nid']})) {
  65. $this->options['alter']['make_link'] = TRUE;
  66. $this->options['alter']['path'] = "node/" . $values->{$this->aliases['nid']};
  67. if (isset($this->aliases['language'])) {
  68. $languages = language_list();
  69. if (isset($languages[$values->{$this->aliases['language']}])) {
  70. $this->options['alter']['language'] = $languages[$values->{$this->aliases['language']}];
  71. }
  72. else {
  73. unset($this->options['alter']['language']);
  74. }
  75. }
  76. }
  77. else {
  78. $this->options['alter']['make_link'] = FALSE;
  79. $this->options['alter']['path'] = "";
  80. }
  81. }
  82. return $data;
  83. }
  84. }