1
|
<?php
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
class EntityReference_SelectionHandler_Views implements EntityReference_SelectionHandler {
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
|
12
|
return new EntityReference_SelectionHandler_Views($field, $instance, $entity);
|
13
|
}
|
14
|
|
15
|
protected function __construct($field, $instance, $entity) {
|
16
|
$this->field = $field;
|
17
|
$this->instance = $instance;
|
18
|
$this->entity = $entity;
|
19
|
}
|
20
|
|
21
|
|
22
|
|
23
|
|
24
|
public static function settingsForm($field, $instance) {
|
25
|
$view_settings = empty($field['settings']['handler_settings']['view']) ? '' : $field['settings']['handler_settings']['view'];
|
26
|
$displays = views_get_applicable_views('entityreference display');
|
27
|
|
28
|
|
29
|
$entity_info = entity_get_info($field['settings']['target_type']);
|
30
|
$options = array();
|
31
|
foreach ($displays as $data) {
|
32
|
list($view, $display_id) = $data;
|
33
|
if ($view->base_table == $entity_info['base table']) {
|
34
|
$options[$view->name . ':' . $display_id] = $view->name . ' - ' . $view->display[$display_id]->display_title;
|
35
|
}
|
36
|
}
|
37
|
|
38
|
|
39
|
|
40
|
|
41
|
|
42
|
$form['view']['#element_validate'] = array('entityreference_view_settings_validate');
|
43
|
|
44
|
if ($options) {
|
45
|
$default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL;
|
46
|
$form['view']['view_and_display'] = array(
|
47
|
'#type' => 'select',
|
48
|
'#title' => t('View used to select the entities'),
|
49
|
'#required' => TRUE,
|
50
|
'#options' => $options,
|
51
|
'#default_value' => $default,
|
52
|
'#description' => '<p>' . t('Choose the view and display that select the entities that can be referenced.<br />Only views with a display of type "Entity Reference" are eligible.') . '</p>',
|
53
|
);
|
54
|
|
55
|
$default = !empty($view_settings['args']) ? implode(', ', $view_settings['args']) : '';
|
56
|
$description = t('Provide a comma separated list of arguments to pass to the view.') . '<br />' . t('This field supports tokens.');
|
57
|
|
58
|
if (!module_exists('token')) {
|
59
|
$description .= '<br>' . t('Install the <a href="@url">token module</a> to get more tokens and display available once.', array('@url' => 'http://drupal.org/project/token'));
|
60
|
}
|
61
|
|
62
|
$form['view']['args'] = array(
|
63
|
'#type' => 'textfield',
|
64
|
'#title' => t('View arguments'),
|
65
|
'#default_value' => $default,
|
66
|
'#required' => FALSE,
|
67
|
'#description' => $description,
|
68
|
'#maxlength' => '512',
|
69
|
);
|
70
|
if (module_exists('token')) {
|
71
|
|
72
|
$info = entity_get_info($instance['entity_type']);
|
73
|
|
74
|
$form['view']['tokens'] = array(
|
75
|
'#theme' => 'token_tree',
|
76
|
'#token_types' => array($info['token type']),
|
77
|
'#global_types' => TRUE,
|
78
|
'#click_insert' => TRUE,
|
79
|
'#dialog' => TRUE,
|
80
|
);
|
81
|
}
|
82
|
}
|
83
|
else {
|
84
|
$form['view']['no_view_help'] = array(
|
85
|
'#markup' => '<p>' . t('No eligible views were found. <a href="@create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href="@existing">existing view</a>.', array(
|
86
|
'@create' => url('admin/structure/views/add'),
|
87
|
'@existing' => url('admin/structure/views'),
|
88
|
)) . '</p>',
|
89
|
);
|
90
|
}
|
91
|
return $form;
|
92
|
}
|
93
|
|
94
|
protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) {
|
95
|
$view_name = $this->field['settings']['handler_settings']['view']['view_name'];
|
96
|
$display_name = $this->field['settings']['handler_settings']['view']['display_name'];
|
97
|
$args = $this->field['settings']['handler_settings']['view']['args'];
|
98
|
$entity_type = $this->field['settings']['target_type'];
|
99
|
|
100
|
|
101
|
$this->view = views_get_view($view_name);
|
102
|
if (!$this->view || !isset($this->view->display[$display_name]) || !$this->view->access($display_name)) {
|
103
|
watchdog('entityreference', 'The view %view_name is no longer eligible for the %field_name field.', array('%view_name' => $view_name, '%field_name' => $this->instance['label']), WATCHDOG_WARNING);
|
104
|
return FALSE;
|
105
|
}
|
106
|
$this->view->set_display($display_name);
|
107
|
$this->view->pre_execute();
|
108
|
|
109
|
|
110
|
$this->view->is_cacheable = FALSE;
|
111
|
|
112
|
|
113
|
$entityreference_options = array(
|
114
|
'match' => $match,
|
115
|
'match_operator' => $match_operator,
|
116
|
'limit' => $limit,
|
117
|
'ids' => $ids,
|
118
|
);
|
119
|
$this->view->display_handler->set_option('entityreference_options', $entityreference_options);
|
120
|
return TRUE;
|
121
|
}
|
122
|
|
123
|
|
124
|
|
125
|
|
126
|
public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
|
127
|
$display_name = $this->field['settings']['handler_settings']['view']['display_name'];
|
128
|
$args = $this->handleArgs($this->field['settings']['handler_settings']['view']['args']);
|
129
|
$result = array();
|
130
|
if ($this->initializeView($match, $match_operator, $limit)) {
|
131
|
|
132
|
$result = $this->view->execute_display($display_name, $args);
|
133
|
}
|
134
|
|
135
|
$return = array();
|
136
|
if ($result) {
|
137
|
$target_type = $this->field['settings']['target_type'];
|
138
|
$entities = entity_load($target_type, array_keys($result));
|
139
|
foreach($entities as $entity) {
|
140
|
list($id,, $bundle) = entity_extract_ids($target_type, $entity);
|
141
|
$return[$bundle][$id] = $result[$id];
|
142
|
}
|
143
|
}
|
144
|
return $return;
|
145
|
}
|
146
|
|
147
|
|
148
|
|
149
|
|
150
|
function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
|
151
|
$this->getReferencableEntities($match, $match_operator);
|
152
|
return $this->view->total_items;
|
153
|
}
|
154
|
|
155
|
function validateReferencableEntities(array $ids) {
|
156
|
$display_name = $this->field['settings']['handler_settings']['view']['display_name'];
|
157
|
$args = $this->handleArgs($this->field['settings']['handler_settings']['view']['args']);
|
158
|
$result = array();
|
159
|
if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) {
|
160
|
|
161
|
$entities = $this->view->execute_display($display_name, $args);
|
162
|
if (!empty($entities)) {
|
163
|
$result = array_keys($entities);
|
164
|
}
|
165
|
}
|
166
|
return $result;
|
167
|
}
|
168
|
|
169
|
|
170
|
|
171
|
|
172
|
public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
|
173
|
return NULL;
|
174
|
}
|
175
|
|
176
|
|
177
|
|
178
|
|
179
|
public function getLabel($entity) {
|
180
|
return entity_label($this->field['settings']['target_type'], $entity);
|
181
|
}
|
182
|
|
183
|
|
184
|
|
185
|
|
186
|
public function entityFieldQueryAlter(SelectQueryInterface $query) {
|
187
|
|
188
|
}
|
189
|
|
190
|
|
191
|
|
192
|
|
193
|
|
194
|
|
195
|
|
196
|
|
197
|
|
198
|
|
199
|
|
200
|
|
201
|
protected function handleArgs($args) {
|
202
|
if (!module_exists('token')) {
|
203
|
return $args;
|
204
|
}
|
205
|
|
206
|
|
207
|
$data = array();
|
208
|
$options = array('clear' => TRUE);
|
209
|
|
210
|
if ($entity = $this->entity) {
|
211
|
|
212
|
|
213
|
|
214
|
|
215
|
|
216
|
$info = entity_get_info($this->instance['entity_type']);
|
217
|
if (!isset($entity->{$info['entity keys']['id']})) {
|
218
|
$entity = clone $entity;
|
219
|
$entity->{$info['entity keys']['id']} = '0';
|
220
|
if (!empty($info['entity keys']['revision'])) {
|
221
|
$entity->{$info['entity keys']['revision']} = '0';
|
222
|
}
|
223
|
}
|
224
|
|
225
|
$data[$info['token type']] = $entity;
|
226
|
}
|
227
|
|
228
|
foreach ($args as $key => $arg) {
|
229
|
$args[$key] = token_replace($arg, $data, $options);
|
230
|
}
|
231
|
return $args;
|
232
|
}
|
233
|
}
|
234
|
|
235
|
function entityreference_view_settings_validate($element, &$form_state, $form) {
|
236
|
|
237
|
if (!empty($element['view_and_display']['#value'])) {
|
238
|
list($view, $display) = explode(':', $element['view_and_display']['#value']);
|
239
|
}
|
240
|
else {
|
241
|
form_error($element, t('The views entity selection mode requires a view.'));
|
242
|
return;
|
243
|
}
|
244
|
|
245
|
|
246
|
|
247
|
|
248
|
$args_string = trim($element['args']['#value']);
|
249
|
if ($args_string === '') {
|
250
|
$args = array();
|
251
|
}
|
252
|
else {
|
253
|
|
254
|
$args = array_map('trim', explode(',', $args_string));
|
255
|
}
|
256
|
|
257
|
$value = array('view_name' => $view, 'display_name' => $display, 'args' => $args);
|
258
|
form_set_value($element, $value, $form_state);
|
259
|
}
|