views_handler_field_entity.inc

Definition of views_handler_field_entity.

File

handlers/views_handler_field_entity.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_entity.
  5. */
  6. /**
  7. * A handler to display data from entity objects.
  8. *
  9. * Fields based upon this handler work with all query-backends if the tables
  10. * used by the query backend have an 'entity type' specified. In order to
  11. * make fields based upon this handler automatically available to all compatible
  12. * query backends, the views field can be defined in the table
  13. * @code views_entity_{ENTITY_TYPE} @endcode.
  14. *
  15. * @ingroup views_field_handlers
  16. */
  17. class views_handler_field_entity extends views_handler_field {
  18. /**
  19. * Stores the entity type which is loaded by this field.
  20. */
  21. public $entity_type;
  22. /**
  23. * Stores all entites which are in the result.
  24. */
  25. public $entities;
  26. /**
  27. * The base field of the entity type assosiated with this field.
  28. */
  29. public $base_field;
  30. /**
  31. * Initialize the entity type.
  32. */
  33. public function init(&$view, &$options) {
  34. parent::init($view, $options);
  35. // Initialize the entity-type used.
  36. $table_data = views_fetch_data($this->table);
  37. $this->entity_type = $table_data['table']['entity type'];
  38. }
  39. /**
  40. * Overriden to add the field for the entity id.
  41. */
  42. function query() {
  43. $this->table_alias = $base_table = $this->view->base_table;
  44. $this->base_field = $this->view->base_field;
  45. if (!empty($this->relationship)) {
  46. foreach ($this->view->relationship as $relationship) {
  47. if ($relationship->alias == $this->relationship) {
  48. $base_table = $relationship->definition['base'];
  49. $this->table_alias = $relationship->alias;
  50. $table_data = views_fetch_data($base_table);
  51. $this->base_field = empty($relationship->definition['base field']) ? $table_data['table']['base']['field'] : $relationship->definition['base field'];
  52. }
  53. }
  54. }
  55. // Add the field if the query back-end implements an add_field() method,
  56. // just like the default back-end.
  57. if (method_exists($this->query, 'add_field')) {
  58. $this->field_alias = $this->query->add_field($this->table_alias, $this->base_field, '');
  59. }
  60. $this->add_additional_fields();
  61. }
  62. /**
  63. * Load the entities for all rows that are about to be displayed.
  64. */
  65. function pre_render(&$values) {
  66. if (!empty($values)) {
  67. list($this->entity_type, $this->entities) = $this->query->get_result_entities($values, !empty($this->relationship) ? $this->relationship : NULL, $this->field_alias);
  68. }
  69. }
  70. /**
  71. * Overridden to return the entity object, or a certain property of the entity.
  72. */
  73. function get_value($values, $field = NULL) {
  74. if (isset($this->entities[$this->view->row_index])) {
  75. $entity = $this->entities[$this->view->row_index];
  76. // Support to get a certain part of the entity.
  77. if (isset($field) && isset($entity->{$field})) {
  78. return $entity->{$field};
  79. }
  80. // Support to get a part of the values as the normal get_value.
  81. elseif (isset($field) && isset($values->{$this->aliases[$field]})) {
  82. return $values->{$this->aliases[$field]};
  83. }
  84. else {
  85. return $entity;
  86. }
  87. }
  88. return FALSE;
  89. }
  90. }