tripal_views_handler_field_element.inc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_element extends tripal_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. $field_name = $this->field_alias;
  35. if (preg_match('/^(.+?)\.(.*)$/', $field_name, $matches)) {
  36. $field_name = $matches[1];
  37. $element_name = $matches[2];
  38. }
  39. if (isset($values->{$field_name})) {
  40. return $values->{$field_name};
  41. }
  42. }
  43. /**
  44. * Render the field.
  45. *
  46. * @param $values
  47. * The values retrieved from the database.
  48. */
  49. function render($values) {
  50. $field_name = $this->field_alias;
  51. $element_name = $field_name;
  52. if (preg_match('/^(.+?)\.(.*)$/', $field_name, $matches)) {
  53. $field_name = $matches[1];
  54. $element_name = $matches[2];
  55. // Conver the element name back to it's correct format with the colon.
  56. $element_name = preg_replace('/__/', ':', $element_name);
  57. }
  58. $value = $this->get_value($values);
  59. // Handle single value fields:
  60. if (count($value) == 0) {
  61. return '';
  62. }
  63. if (count($value) == 1) {
  64. return $this->sanitize_value($value[0]['value'][$element_name], 'xss');
  65. }
  66. else {
  67. dpm($value);
  68. return t('Too many values to show.');
  69. }
  70. }
  71. protected function get_element_value($value, $element_name) {
  72. }
  73. }