1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Handler for entityreference_plugin_display.
|
6
|
*/
|
7
|
class entityreference_plugin_display extends views_plugin_display {
|
8
|
|
9
|
function option_definition() {
|
10
|
$options = parent::option_definition();
|
11
|
|
12
|
// Force the style plugin to 'entityreference_style' and the row plugin to
|
13
|
// 'fields'.
|
14
|
$options['style_plugin']['default'] = 'entityreference_style';
|
15
|
$options['defaults']['default']['style_plugin'] = FALSE;
|
16
|
$options['defaults']['default']['style_options'] = FALSE;
|
17
|
$options['row_plugin']['default'] = 'entityreference_fields';
|
18
|
$options['defaults']['default']['row_plugin'] = FALSE;
|
19
|
$options['defaults']['default']['row_options'] = FALSE;
|
20
|
|
21
|
// Set the display title to an empty string (not used in this display type).
|
22
|
$options['title']['default'] = '';
|
23
|
$options['defaults']['default']['title'] = FALSE;
|
24
|
|
25
|
return $options;
|
26
|
}
|
27
|
|
28
|
function get_style_type() {
|
29
|
return 'entityreference';
|
30
|
}
|
31
|
|
32
|
function execute() {
|
33
|
return $this->view->render($this->display->id);
|
34
|
}
|
35
|
|
36
|
function render() {
|
37
|
if (!empty($this->view->result) || !empty($this->view->style_plugin->definition['even empty'])) {
|
38
|
return $this->view->style_plugin->render($this->view->result);
|
39
|
}
|
40
|
return '';
|
41
|
}
|
42
|
|
43
|
function uses_exposed() {
|
44
|
return FALSE;
|
45
|
}
|
46
|
|
47
|
function query() {
|
48
|
$options = $this->get_option('entityreference_options');
|
49
|
|
50
|
// Play nice with Views UI 'preview' : if the view is not executed through
|
51
|
// EntityReference_SelectionHandler_Views::getReferencableEntities(),
|
52
|
// don't alter the query.
|
53
|
if (empty($options)) {
|
54
|
return;
|
55
|
}
|
56
|
|
57
|
// Make sure the id field is included in the results, and save its alias
|
58
|
// so that references_plugin_style can retrieve it.
|
59
|
$this->id_field_alias = $id_field = $this->view->query->add_field($this->view->base_table, $this->view->base_field);
|
60
|
if (strpos($id_field, '.') === FALSE) {
|
61
|
$id_field = $this->view->base_table . '.' . $this->id_field_alias;
|
62
|
}
|
63
|
|
64
|
// Restrict the autocomplete options based on what's been typed already.
|
65
|
if (isset($options['match'])) {
|
66
|
$style_options = $this->get_option('style_options');
|
67
|
$value = db_like($options['match']) . '%';
|
68
|
if ($options['match_operator'] != 'STARTS_WITH') {
|
69
|
$value = '%' . $value;
|
70
|
}
|
71
|
|
72
|
// Multiple search fields are OR'd together
|
73
|
$conditions = db_or();
|
74
|
|
75
|
// Build the condition using the selected search fields
|
76
|
foreach ($style_options['search_fields'] as $field_alias) {
|
77
|
if (!empty($field_alias)) {
|
78
|
|
79
|
// Get the table and field names for the checked field.
|
80
|
if (empty($this->view->field[$field_alias]->field_info)) {
|
81
|
$field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
|
82
|
}
|
83
|
else {
|
84
|
$this->view->query->add_field($this->view->field[$field_alias]->options['table'], $this->view->field[$field_alias]->real_field, $this->view->field[$field_alias]->options['field'], array());
|
85
|
$field = $this->view->query->fields[$this->view->field[$field_alias]->options['field']];
|
86
|
}
|
87
|
// Add an OR condition for the field
|
88
|
$conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
|
89
|
}
|
90
|
}
|
91
|
|
92
|
$this->view->query->add_where(NULL, $conditions);
|
93
|
}
|
94
|
|
95
|
// Add an IN condition for validation.
|
96
|
if (!empty($options['ids'])) {
|
97
|
$this->view->query->add_where(NULL, $id_field, $options['ids']);
|
98
|
}
|
99
|
|
100
|
$this->view->set_items_per_page($options['limit']);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Extend the default validation.
|
105
|
*/
|
106
|
function validate() {
|
107
|
$errors = parent::validate();
|
108
|
// Verify that search fields are set up.
|
109
|
$style_options = $this->get_option('style_options');
|
110
|
if (!isset($style_options['search_fields'])) {
|
111
|
$errors[] = t('Display "@display" needs a selected search fields to work properly. See the settings for the Entity Reference list format.', array('@display' => $this->display->display_title));
|
112
|
}
|
113
|
else {
|
114
|
// Verify that the search fields used actually exist.
|
115
|
//$fields = array_keys($this->view->get_items('field'));
|
116
|
$fields = array_keys($this->handlers['field']);
|
117
|
foreach ($style_options['search_fields'] as $field_alias => $enabled) {
|
118
|
if ($enabled && !in_array($field_alias, $fields)) {
|
119
|
$errors[] = t('Display "@display" uses field %field as search field, but the field is no longer present. See the settings for the Entity Reference list format.', array('@display' => $this->display->display_title, '%field' => $field_alias));
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
return $errors;
|
124
|
}
|
125
|
}
|