views_handler_field_entity.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. if (isset($table_data['table']['entity type'])) {
  38. $this->entity_type = $table_data['table']['entity type'];
  39. }
  40. }
  41. /**
  42. * Overriden to add the field for the entity id.
  43. */
  44. public function query() {
  45. $this->table_alias = $base_table = $this->view->base_table;
  46. $this->base_field = $this->view->base_field;
  47. if (!empty($this->relationship)) {
  48. foreach ($this->view->relationship as $relationship) {
  49. if ($relationship->alias == $this->relationship) {
  50. $base_table = $relationship->definition['base'];
  51. $this->table_alias = $relationship->alias;
  52. $table_data = views_fetch_data($base_table);
  53. $this->base_field = empty($relationship->definition['base field']) ? $table_data['table']['base']['field'] : $relationship->definition['base field'];
  54. }
  55. }
  56. }
  57. // Add the field if the query back-end implements an add_field() method,
  58. // just like the default back-end.
  59. if (method_exists($this->query, 'add_field')) {
  60. $this->field_alias = $this->query->add_field($this->table_alias, $this->base_field, '');
  61. }
  62. $this->add_additional_fields();
  63. }
  64. /**
  65. * Load the entities for all rows that are about to be displayed.
  66. */
  67. public function pre_render(&$values) {
  68. if (!empty($values)) {
  69. list($this->entity_type, $this->entities) = $this->query->get_result_entities($values, !empty($this->relationship) ? $this->relationship : NULL, $this->field_alias);
  70. }
  71. }
  72. /**
  73. * Return the entity object or a certain property of the entity.
  74. */
  75. public function get_value($values, $field = NULL) {
  76. if (isset($this->entities[$this->view->row_index])) {
  77. $entity = $this->entities[$this->view->row_index];
  78. // Support to get a certain part of the entity.
  79. if (isset($field) && isset($entity->{$field})) {
  80. return $entity->{$field};
  81. }
  82. // Support to get a part of the values as the normal get_value.
  83. elseif (isset($field) && isset($values->{$this->aliases[$field]})) {
  84. return $values->{$this->aliases[$field]};
  85. }
  86. else {
  87. return $entity;
  88. }
  89. }
  90. return FALSE;
  91. }
  92. }