1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Row style plugin for displaying the results as entities.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Plugin class for displaying Views results with entity_view.
|
10
|
*/
|
11
|
class entity_views_plugin_row_entity_view extends views_plugin_row {
|
12
|
|
13
|
protected $entity_type, $entities;
|
14
|
|
15
|
public function init(&$view, &$display, $options = NULL) {
|
16
|
parent::init($view, $display, $options);
|
17
|
|
18
|
// Initialize the entity-type used.
|
19
|
$table_data = views_fetch_data($this->view->base_table);
|
20
|
$this->entity_type = $table_data['table']['entity type'];
|
21
|
// Set base table and field information as used by views_plugin_row to
|
22
|
// select the entity id if used with default query class.
|
23
|
$info = entity_get_info($this->entity_type);
|
24
|
if (!empty($info['base table']) && $info['base table'] == $this->view->base_table) {
|
25
|
$this->base_table = $info['base table'];
|
26
|
$this->base_field = $info['entity keys']['id'];
|
27
|
}
|
28
|
}
|
29
|
|
30
|
public function option_definition() {
|
31
|
$options = parent::option_definition();
|
32
|
$options['view_mode'] = array('default' => 'full');
|
33
|
return $options;
|
34
|
}
|
35
|
|
36
|
public function options_form(&$form, &$form_state) {
|
37
|
parent::options_form($form, $form_state);
|
38
|
|
39
|
$entity_info = entity_get_info($this->entity_type);
|
40
|
$options = array();
|
41
|
if (!empty($entity_info['view modes'])) {
|
42
|
foreach ($entity_info['view modes'] as $mode => $settings) {
|
43
|
$options[$mode] = $settings['label'];
|
44
|
}
|
45
|
}
|
46
|
|
47
|
if (count($options) > 1) {
|
48
|
$form['view_mode'] = array(
|
49
|
'#type' => 'select',
|
50
|
'#options' => $options,
|
51
|
'#title' => t('View mode'),
|
52
|
'#default_value' => $this->options['view_mode'],
|
53
|
);
|
54
|
}
|
55
|
else {
|
56
|
$form['view_mode_info'] = array(
|
57
|
'#type' => 'item',
|
58
|
'#title' => t('View mode'),
|
59
|
'#description' => t('Only one view mode is available for this entity type.'),
|
60
|
'#markup' => $options ? current($options) : t('Default'),
|
61
|
);
|
62
|
$form['view_mode'] = array(
|
63
|
'#type' => 'value',
|
64
|
'#value' => $options ? key($options) : 'default',
|
65
|
);
|
66
|
}
|
67
|
return $form;
|
68
|
}
|
69
|
|
70
|
public function pre_render($values) {
|
71
|
if (!empty($values)) {
|
72
|
list($this->entity_type, $this->entities) = $this->view->query->get_result_entities($values, !empty($this->relationship) ? $this->relationship : NULL, isset($this->field_alias) ? $this->field_alias : NULL);
|
73
|
}
|
74
|
// Render the entities.
|
75
|
if ($this->entities) {
|
76
|
$render = entity_view($this->entity_type, $this->entities, $this->options['view_mode']);
|
77
|
// Remove the first level of the render array.
|
78
|
$this->rendered_content = reset($render);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Overridden to return the entity object.
|
84
|
*/
|
85
|
function get_value($values, $field = NULL) {
|
86
|
return isset($this->entities[$this->view->row_index]) ? $this->entities[$this->view->row_index] : FALSE;
|
87
|
}
|
88
|
|
89
|
public function render($values) {
|
90
|
if ($entity = $this->get_value($values)) {
|
91
|
// Add the view object as views_plugin_row_node_view::render() would.
|
92
|
// Otherwise the views theme suggestions won't work properly.
|
93
|
$entity->view = $this->view;
|
94
|
$render = $this->rendered_content[entity_id($this->entity_type, $entity)];
|
95
|
return drupal_render($render);
|
96
|
}
|
97
|
}
|
98
|
}
|