tripal_views_handler_field.inc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. * Get the value that's supposed to be rendered.
  24. *
  25. * This api exists so that other modules can easy set the values of the field
  26. * without having the need to change the render method as well.
  27. *
  28. * @param $values
  29. * An object containing all retrieved values.
  30. * @param $field
  31. * Optional name of the field where the value is stored.
  32. */
  33. function get_value($values, $field = NULL) {
  34. $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
  35. // For some reason the alias isn't being added to the $this->aliases
  36. // variable when the user clicks the checkbox 'Link this field to the
  37. // original piece of content'. That may need to be investigated and is
  38. // probably a side effect of using the views_handler_field as a parent
  39. // class that is expecting something more for SQL. We don't use aliases
  40. // for fields so the following if statement will fix the problem.
  41. if (!$alias) {
  42. $alias = $this->field_alias;
  43. }
  44. if (isset($values->{$alias})) {
  45. return $values->{$alias};
  46. }
  47. }
  48. /**
  49. * Render the field.
  50. *
  51. * @param $values
  52. * The values retrieved from the database.
  53. */
  54. function render($values) {
  55. $value = $this->get_value($values);
  56. return $this->sanitize_value($value);
  57. }
  58. }