Projet

Général

Profil

Paste
Télécharger (3,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_entity.inc @ 5d12d676

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
    $this->entity_type = $table_data['table']['entity type'];
45
  }
46

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

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

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

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

    
72
    $this->add_additional_fields();
73
  }
74

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

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

    
105
}