tripal_views_handler_field.inc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @file
  4. * Views field handler for basic TripalFields fields.
  5. */
  6. /**
  7. * Views field handler for basic TripalFields fields.
  8. */
  9. class tripal_views_handler_field extends views_handler_field {
  10. /**
  11. *
  12. */
  13. function query() {
  14. parent::query();
  15. // We need to add an alias to our TripalFields so that the
  16. // views can find the results. With SQL it sets the alias for each
  17. // field and expects to find that alias in the results array. Without
  18. // setting this alias Views can't find our results from our
  19. // tripal_views_query plugin.
  20. $this->field_alias = $this->real_field;
  21. }
  22. /**
  23. * Overrides click_sort().
  24. */
  25. function click_sort($order) {
  26. if (isset($this->field_alias)) {
  27. // Since fields should always have themselves already added, just
  28. // add a sort on the field.
  29. $params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : [];
  30. $this->query->add_orderby($this->table_alias, $this->real_field, $order, $this->field_alias, $params);
  31. }
  32. }
  33. /**
  34. * Get the value that's supposed to be rendered.
  35. *
  36. * This api exists so that other modules can easy set the values of the field
  37. * without having the need to change the render method as well.
  38. *
  39. * @param $values
  40. * An object containing all retrieved values.
  41. * @param $field
  42. * Optional name of the field where the value is stored.
  43. */
  44. function get_value($values, $field = NULL) {
  45. $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
  46. // For some reason the alias isn't being added to the $this->aliases
  47. // variable when the user clicks the checkbox 'Link this field to the
  48. // original piece of content'. That may need to be investigated and is
  49. // probably a side effect of using the views_handler_field as a parent
  50. // class that is expecting something more for SQL. We don't use aliases
  51. // for fields so the following if statement will fix the problem.
  52. if (!$alias) {
  53. $alias = $this->field_alias;
  54. }
  55. if (isset($values->{$alias})) {
  56. return $values->{$alias};
  57. }
  58. }
  59. /**
  60. * Render the field.
  61. *
  62. * @param $values
  63. * The values retrieved from the database.
  64. */
  65. function render($values) {
  66. list ($vocabulary, $accession) = explode('__', $this->table_alias);
  67. $term = tripal_load_term_entity([
  68. 'vocabulary' => $vocabulary,
  69. 'accession' => $accession,
  70. ]);
  71. $bundle = tripal_load_bundle_entity(['term_id' => $term->id]);
  72. $field_name = $this->field_alias;
  73. $field = field_info_field($field_name);
  74. $instance = field_info_instance('TripalEntity', $field_name, $bundle->name);
  75. $entity = $values->entity;
  76. $items = $this->get_value($values);
  77. // Handle single value fields:
  78. if (count($items) == 1) {
  79. $function = $field['module'] . '_field_formatter_view';
  80. $value = $function('TripalEntity', $entity, $field, $instance, 'und', $items, $instance['display']['default']);
  81. return $value;
  82. }
  83. return '';
  84. }
  85. }