Projet

Général

Profil

Paste
Télécharger (6,01 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entity / views / handlers / entity_views_handler_field_entity.inc @ 7d7b5830

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the entity_views_handler_field_entity class.
6
 */
7

    
8
/**
9
 * A handler to provide proper displays for entities retrieved via data selection.
10
 *
11
 * This handler may only be used in conjunction with data selection based Views
12
 * tables or other base tables using a query plugin that supports data
13
 * selection.
14
 *
15
 * @see entity_views_field_definition()
16
 * @ingroup views_field_handlers
17
 */
18
class entity_views_handler_field_entity extends views_handler_field {
19

    
20
  /**
21
   * Stores the entity type of the result entities.
22
   */
23
  public $entity_type;
24

    
25
  /**
26
   * Stores the result entities' metadata wrappers.
27
   */
28
  public $wrappers = array();
29

    
30
  /**
31
   * The entity type of the entity displayed by this field.
32
   */
33
  public $field_entity_type;
34

    
35
  /**
36
   * Stores the current value when rendering list fields.
37
   */
38
  public $current_value;
39

    
40
  /**
41
   * Initialize the entity type with the field's entity type.
42
   */
43
  public function init(&$view, &$options) {
44
    parent::init($view, $options);
45
    $this->field_entity_type = entity_property_extract_innermost_type($this->definition['type']);
46
  }
47

    
48
  /**
49
   * Overridden to add the field for the entity ID (if necessary).
50
   */
51
  public function query() {
52
    EntityFieldHandlerHelper::query($this);
53
  }
54

    
55
  /**
56
   * Adds a click-sort to the query.
57
   */
58
  public function click_sort($order) {
59
    EntityFieldHandlerHelper::click_sort($this, $order);
60
  }
61

    
62
  /**
63
   * Load the entities for all rows that are about to be displayed.
64
   */
65
  public function pre_render(&$values) {
66
    EntityFieldHandlerHelper::pre_render($this, $values);
67
  }
68

    
69
  /**
70
   * Overridden to use a metadata wrapper.
71
   */
72
  public function get_value($values, $field = NULL) {
73
    return EntityFieldHandlerHelper::get_value($this, $values, $field);
74
  }
75

    
76
  public function option_definition() {
77
    $options = parent::option_definition();
78
    $options += EntityFieldHandlerHelper::option_definition($this);
79

    
80
    $options['display'] = array('default' => 'label');
81
    $options['link_to_entity']['default'] = TRUE;
82
    $options['view_mode'] = array('default' => 'default');
83
    $options['bypass_access'] = array('default' => FALSE);
84

    
85
    return $options;
86
  }
87

    
88
  public function options_form(&$form, &$form_state) {
89
    parent::options_form($form, $form_state);
90
    EntityFieldHandlerHelper::options_form($this, $form, $form_state);
91
    // We want a different form field at a different place.
92
    unset($form['link_to_entity']);
93

    
94
    $options = array(
95
      'label' => t('Show entity label'),
96
      'id' => t('Show entity ID'),
97
      'view' => t('Show complete entity'),
98
    );
99
    $form['display'] = array(
100
      '#type' => 'select',
101
      '#title' => t('Display'),
102
      '#description' => t('Decide how this field will be displayed.'),
103
      '#options' => $options,
104
      '#default_value' => $this->options['display'],
105
    );
106
    $form['link_to_entity'] = array(
107
      '#type' => 'checkbox',
108
      '#title' => t('Link to entity'),
109
      '#description' => t('Link this field to the entity.'),
110
      '#default_value' => $this->options['link_to_entity'],
111
      '#dependency' => array('edit-options-display' => array('label', 'id')),
112
    );
113

    
114
    // Stolen from entity_views_plugin_row_entity_view.
115
    $entity_info = entity_get_info($this->field_entity_type);
116
    $options = array();
117
    if (!empty($entity_info['view modes'])) {
118
      foreach ($entity_info['view modes'] as $mode => $settings) {
119
        $options[$mode] = $settings['label'];
120
      }
121
    }
122

    
123
    if (count($options) > 1) {
124
      $form['view_mode'] = array(
125
        '#type' => 'select',
126
        '#options' => $options,
127
        '#title' => t('View mode'),
128
        '#default_value' => $this->options['view_mode'],
129
        '#dependency' => array('edit-options-display' => array('view')),
130
      );
131
    }
132
    else {
133
      $form['view_mode'] = array(
134
        '#type' => 'value',
135
        '#value' => $options ? key($options) : 'default',
136
      );
137
    }
138
    $form['bypass_access'] = array(
139
      '#type' => 'checkbox',
140
      '#title' => t('Bypass access checks'),
141
      '#description' => t('If enabled, access permissions for rendering the entity are not checked.'),
142
      '#default_value' => !empty($this->options['bypass_access']),
143
    );
144
  }
145

    
146
  public function render($values) {
147
    return EntityFieldHandlerHelper::render($this, $values);
148
  }
149

    
150
  /**
151
   * Render a value as a link to the entity if applicable.
152
   *
153
   * @param $value
154
   *   The value to render.
155
   * @param $values
156
   *   The values for the current row retrieved from the Views query, as an
157
   *   object.
158
   */
159
  public function render_entity_link($entity, $values) {
160
    $type = $this->field_entity_type;
161
    if (!is_object($entity) && isset($entity) && $entity !== FALSE) {
162
      $entity = entity_load_single($type, $entity);
163
    }
164
    if (!$entity) {
165
      return '';
166
    }
167
    $render = $this->render_single_value($entity, $values);
168
    if (!$this->options['link_to_entity'] || $this->options['display'] == 'view') {
169
      return $render;
170
    }
171
    if (is_object($entity) && ($url = entity_uri($type, $entity))) {
172
      return l($render, $url['path'], array('html' => TRUE) + $url['options']);
173
    }
174
    return $render;
175
  }
176

    
177
  /**
178
   * Render a single field value.
179
   */
180
  public function render_single_value($entity, $values) {
181
    $type = $this->field_entity_type;
182
    if (!is_object($entity) && isset($entity) && $entity !== FALSE) {
183
      $entity = entity_load_single($type, $entity);
184
    }
185
    // Make sure the entity exists and access is either given or bypassed.
186
    if (!$entity || !(!empty($this->options['bypass_access']) || entity_access('view', $type, $entity))) {
187
      return '';
188
    }
189

    
190
    if ($this->options['display'] === 'view') {
191
      $entity_view = entity_view($type, array($entity), $this->options['view_mode']);
192
      return render($entity_view);
193
    }
194

    
195
    if ($this->options['display'] == 'label') {
196
      $value = entity_label($type, $entity);
197
    }
198
    // Either $options[display] == 'id', or we have no label.
199
    if (empty($value)) {
200
      $value = entity_id($type, $entity);
201
    }
202
    $value = $this->sanitize_value($value);
203

    
204
    return $value;
205
  }
206

    
207
}