Project

General

Profile

Paste
Download (3.16 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_entity.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_entity.
6
 */
7

    
8
/**
9
 * A handler to display data from entity objects.
10
 *
11
 * Fields based upon this handler work with all query-backends if the tables
12
 * used by the query backend have an 'entity type' specified. In order to
13
 * make fields based upon this handler automatically available to all compatible
14
 * query backends, the views field can be defined in the table.
15
 * @code views_entity_{ENTITY_TYPE} @endcode.
16
 *
17
 * @ingroup views_field_handlers
18
 */
19
class views_handler_field_entity extends views_handler_field {
20

    
21
  /**
22
   * Stores the entity type which is loaded by this field.
23
   */
24
  public $entity_type;
25

    
26
  /**
27
   * Stores all entites which are in the result.
28
   */
29
  public $entities;
30

    
31
  /**
32
   * The base field of the entity type assosiated with this field.
33
   */
34
  public $base_field;
35

    
36
  /**
37
   * Initialize the entity type.
38
   */
39
  public function init(&$view, &$options) {
40
    parent::init($view, $options);
41

    
42
    // Initialize the entity-type used.
43
    $table_data = views_fetch_data($this->table);
44
    if (isset($table_data['table']['entity type'])) {
45
      $this->entity_type = $table_data['table']['entity type'];
46
    }
47
  }
48

    
49
  /**
50
   * Overriden to add the field for the entity id.
51
   */
52
  public function query() {
53
    $this->table_alias = $base_table = $this->view->base_table;
54
    $this->base_field = $this->view->base_field;
55

    
56
    if (!empty($this->relationship)) {
57
      foreach ($this->view->relationship as $relationship) {
58
        if ($relationship->alias == $this->relationship) {
59
          $base_table = $relationship->definition['base'];
60
          $this->table_alias = $relationship->alias;
61

    
62
          $table_data = views_fetch_data($base_table);
63
          $this->base_field = empty($relationship->definition['base field']) ? $table_data['table']['base']['field'] : $relationship->definition['base field'];
64
        }
65
      }
66
    }
67

    
68
    // Add the field if the query back-end implements an add_field() method,
69
    // just like the default back-end.
70
    if (method_exists($this->query, 'add_field')) {
71
      $this->field_alias = $this->query->add_field($this->table_alias, $this->base_field, '');
72
    }
73

    
74
    $this->add_additional_fields();
75
  }
76

    
77
  /**
78
   * Load the entities for all rows that are about to be displayed.
79
   */
80
  public function pre_render(&$values) {
81
    if (!empty($values)) {
82
      list($this->entity_type, $this->entities) = $this->query->get_result_entities($values, !empty($this->relationship) ? $this->relationship : NULL, $this->field_alias);
83
    }
84
  }
85

    
86
  /**
87
   * Return the entity object or a certain property of the entity.
88
   */
89
  public function get_value($values, $field = NULL) {
90
    if (isset($this->entities[$this->view->row_index])) {
91
      $entity = $this->entities[$this->view->row_index];
92
      // Support to get a certain part of the entity.
93
      if (isset($field) && isset($entity->{$field})) {
94
        return $entity->{$field};
95
      }
96
      // Support to get a part of the values as the normal get_value.
97
      elseif (isset($field) && isset($values->{$this->aliases[$field]})) {
98
        return $values->{$this->aliases[$field]};
99
      }
100
      else {
101
        return $entity;
102
      }
103
    }
104
    return FALSE;
105
  }
106

    
107
}