-
Notifications
You must be signed in to change notification settings - Fork 4
/
references_dialog.dialog_widgets.inc
394 lines (369 loc) · 12.7 KB
/
references_dialog.dialog_widgets.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* @file
* Implements dialogs for node reference and user reference fields.
*/
/**
* Implements hook_references_dialog_widgets().
*/
function references_dialog_references_dialog_widgets() {
return array(
'node_reference_autocomplete' => array(
'element_type' => 'textfield',
'dialog_form' => 'node_reference_dialog_form',
'entity_type' => 'node',
'format' => '$label [nid: $entity_id]',
'views_query' => 'references_dialog_node_reference_views_query',
'operations' => array(
'search' => array(
'function' => 'references_dialog_get_field_search_links',
'title' => t('Search Dialog'),
),
'edit' => array(
'function' => 'references_dialog_node_reference_edit_link',
'title' => t('Edit dialog'),
),
'add' => array(
'function' => 'references_dialog_node_reference_add_link',
'title' => t('Add dialog'),
),
),
),
'user_reference_autocomplete' => array(
'element_type' => 'textfield',
'entity_type' => 'user',
'format' => '$label [uid: $entity_id]',
'operations' => array(
'search' => array(
'function' => 'references_dialog_get_field_search_links',
'title' => t('Search Dialog'),
),
'edit' => array(
'function' => 'references_dialog_user_reference_edit_link',
'title' => t('Edit dialog'),
),
'add' => array(
'function' => 'references_dialog_user_reference_add_link',
'title' => t('Add dialog'),
),
),
),
'entityreference_autocomplete' => array(
'element_type' => 'textfield',
'dialog_form' => 'entityreference_autocomplete_dialog_form',
'views_query' => 'references_dialog_entityreference_views_query',
'type_callback' => 'references_dialog_entityreference_get_type',
'format' => '$label ($entity_id)',
'operations' => array(
'edit' => array(
'function' => 'references_dialog_entityreference_edit_link',
'title' => t('Edit dialog'),
),
'add' => array(
'function' => 'references_dialog_entityreference_add_link',
'title' => t('Add dialog'),
),
'search' => array(
'function' => 'references_dialog_get_field_search_links',
'title' => t('Search Dialog'),
),
),
),
'references_dialog_term_reference' => array(
'element_type' => 'textfield',
'dialog_form' => 'term_reference_dialog_form',
'entity_type' => 'taxonomy_term',
'format' => '$label',
'views_query' => 'references_dialog_term_reference_views_query',
'operations' => array(
'search' => array(
'function' => 'references_dialog_get_field_search_links',
'title' => t('Search Dialog'),
),
'edit' => array(
'function' => 'references_dialog_term_reference_edit_link',
'title' => t('Edit dialog'),
),
'add' => array(
'function' => 'references_dialog_term_reference_add_link',
'title' => t('Add dialog'),
),
),
),
);
}
/**
* Edit link callback for node references.
*/
function references_dialog_node_reference_edit_link($element, $widget_settings, $field, $instance) {
if (isset($element['#default_value']) || isset($element['#value'])) {
if (isset($element['#default_value'])) {
$value = $element['#default_value'];
} else {
// Take "label [nid:id]', match the id from parenthesis.
if (preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $element['#value'], $matches)) {
list(, $title, $value) = $matches;
} else {
return array();
}
}
$node = node_load($value);
if (node_access('update', $node)) {
return array(
array(
'title' => t('Edit'),
'href' => 'node/' . $value . '/edit'
),
);
}
}
return array();
}
/**
* Add link callback for node references.
*/
function references_dialog_node_reference_add_link($element, $widget_settings, $field, $instance) {
// Hide add link for non-empty default value.
if (!empty($element['#default_value']) || !empty($element['#value'])) {
return array();
}
$add_links = array();
foreach ($field['settings']['referenceable_types'] as $type => $active) {
if ($active !== 0) {
$node_type = node_type_load($type);
if ($node_type && node_access('create', $node_type->type)) {
$add_links[] = array(
'title' => t('Create @type', array('@type' => $node_type->name)),
'href' => 'node/add/' . strtr($type, array('_' => '-')),
);
}
}
}
return $add_links;
}
/**
* View query callback for node references.
*/
function references_dialog_node_reference_views_query($view, $instance, $field) {
// We need to make sure that no entries that we can't add to our field shows
// up, so we need to limit the data here.
$types = array();
foreach ($field['settings']['referenceable_types'] as $type => $active) {
if ($active !== 0) {
$types[] = $type;
}
}
$view->query->add_where(0, "$view->base_table.type", $types);
}
/**
* Edit callback for user references.
*/
function references_dialog_user_reference_edit_link($element, $widget_settings, $field, $instance) {
if (!isset($element['#default_value']) && !isset($element['#value'])) {
return array();
}
if (isset($element['#default_value'])) {
$value = $element['#default_value'];
} else {
// Take "label [uid:id]', match the id from parenthesis.
if (preg_match('/^(?:\s*|(.*) )?\[\s*uid\s*:\s*(\d+)\s*\]$/', $element['#value'], $matches)) {
list(, $title, $value) = $matches;
} else {
return array();
}
}
global $user;
$user_links = array();
if (is_numeric($value) && user_load($value) &&
user_access('access user profiles') &&
(user_access('administer users') || $user->uid == $value)) {
$user_links[] = array(
'title' => t('Edit'),
'href' => 'user/' . $value . '/edit',
);
}
return $user_links;
}
/**
* Add link callback for user references.
*/
function references_dialog_user_reference_add_link($element, $widget_settings, $field, $instance) {
// Hide add link for non-empty default value.
if (!empty($element['#default_value']) || !empty($element['#value'])) {
return array();
}
$user_links = array();
// Check permissions for adding users.
if (user_access('administer users')) {
$user_links[] = array(
'title' => t('Create user'),
'href' => 'admin/people/create'
);
}
return $user_links;
}
/**
* Edit link callback for entity references.
*/
function references_dialog_entityreference_edit_link($element, $widget_settings, $field, $instance) {
//if (isset($element['#default_value'])) {
$raw_value = $element['#default_value'] ? $element['#default_value'] : $element['#value'];
$entity_type = $field['settings']['target_type'];
// Take "label (entity id)', match the id from parenthesis.
if (preg_match("/.+\((\d+)\)/", $raw_value, $matches)) {
$value = $matches[1];
}
if (isset($value)) {
$entity = current(entity_load($entity_type, array($value)));
if (entity_access('update', $entity_type, $entity)) {
return array(
array(
'title' => t('Edit'),
'href' => references_dialog_get_admin_path($entity_type, 'edit', NULL, $entity),
),
);
}
}
//}
return array();
}
/**
* Add link callback for entity references.
*/
function references_dialog_entityreference_add_link($element, $widget_settings, $field, $instance) {
// Hide add link for non-empty default value.
if (!empty($element['#default_value']) || !empty($element['#value'])) {
return array();
}
$add_links = array();
$entity_type = $field['settings']['target_type'];
$entity_info = entity_get_info($entity_type);
$entity_bundles = array_keys($entity_info['bundles']);
if (!empty($field['settings']['handler_settings']['target_bundles'])) {
$bundles = $field['settings']['handler_settings']['target_bundles'];
}
elseif (!empty($field['settings']['handler_settings']['view'])) {
$name = $field['settings']['handler_settings']['view']['view_name'];
$display = $field['settings']['handler_settings']['view']['display_name'];
$views = views_get_view($name);
$views_display = $views->display;
if (isset($views_display[$display]->display_options['filters']['type'])) {
$views_filters = $views_display[$display]->display_options['filters']['type']['value'];
}
else {
$views_filters = $views_display['default']->display_options['filters']['type']['value'];
}
$bundles = array_keys($views_filters);
}
elseif (isset($entity_info['bundles'])) {
// If the entity target bundles is empty, it means all target bundles are allowed. Fill it all up!
$bundles = $entity_bundles;
}
// Create a link for each allowed bundles.
if (isset($bundles)) {
foreach ($bundles as $bundle) {
if (in_array($bundle, $entity_bundles) && $link = references_dialog_entityreference_link_helper($entity_type, $bundle)) {
$add_links[] = $link;
}
}
}
return $add_links;
}
function references_dialog_entityreference_link_helper($entity_type, $bundle = NULL) {
$wrapper = entity_metadata_wrapper($entity_type, NULL, array('bundle' => $bundle));
$info = $wrapper->entityInfo();
if (isset($bundle)) {
$label = $info['bundles'][$bundle]['label'];
}
else {
$label = $info['label'];
}
// entity_access() doesn't provide a generic bundle create op access check.
switch ($entity_type) {
case 'node':
$access = node_access('create', $bundle);
break;
default:
$access = entity_access('create', $entity_type);
break;
}
$path = references_dialog_get_admin_path($entity_type, 'add', $bundle);
if ($access && $path) {
$link = array(
'title' => t('Create @type', array('@type' => $label)),
'href' => $path,
);
return $link;
}
return FALSE;
}
/**
* View query callback for entityreference references.
*/
function references_dialog_entityreference_views_query($view, $instance, $field) {
// We need to make sure that no entries that we can't add to our field shows
// up, so we need to limit the data here.
$types = array();
if (!empty($field['settings']['handler_settings']['target_bundles'])) {
$entity_info = entity_get_info($field['settings']['target_type']);
if (isset($entity_info['entity keys']['bundle'])) {
// Taxonomy terms don't have their bundle in the table, so we handle them
// specially, for now.
if ($field['settings']['target_type'] == 'taxonomy_term') {
$table = 'taxonomy_vocabulary';
$column = 'machine_name';
}
else {
$table = $view->base_table;
$column = $entity_info['entity keys']['bundle'];
}
// Add the bundle property as a default.
$view->query->add_where(0, "$table." . $column, array_values($field['settings']['handler_settings']['target_bundles']));
}
}
}
function references_dialog_entityreference_get_type($instance, $field) {
return $field['settings']['target_type'];
}
/**
* Edit link callback for term references.
*/
function references_dialog_term_reference_edit_link($element, $widget_settings, $field, $instance) {
if (isset($element['#default_value'])) {
// Get Term ID from entity.
$term = taxonomy_term_load($element['#entity']->{$element['#field_name']}[$element['#language']][$element['#delta']]['tid']);
if ($term && user_access('administer taxonomy')) {
$path = entity_uri('taxonomy_term', $term);
return array(
array(
'title' => t('Edit'),
'href' => $path['path'] . '/edit'
),
);
}
}
return array();
}
/**
* Add link callback for term references.
*/
function references_dialog_term_reference_add_link($element, $widget_settings, $field, $instance) {
$add_links = array();
if (user_access('administer taxonomy')) {
$add_links[] = array(
'title' => t('Create @type', array('@type' => strtoupper($field['settings']['allowed_values'][0]['vocabulary']))),
'href' => 'admin/structure/taxonomy/' . $field['settings']['allowed_values'][0]['vocabulary'] . '/add',
);
}
return $add_links;
}
/**
* View query callback for term references.
*/
function references_dialog_term_reference_views_query($view, $instance, $field) {
// We need to make sure that no entries that we can't add to our field shows
// up, so we need to limit the data here.
$vocabulary = taxonomy_vocabulary_load($field['settings']['allowed_values'][0]['vocabulary']);
$types = array($vocabulary->machine_name);
$view->query->add_where(0, "$view->base_table.vocabulary", $types);
}