-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathting_reference.module
523 lines (461 loc) · 15.9 KB
/
ting_reference.module
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
/**
* @file
* Code for the Ting object reference feature.
*/
include_once 'ting_reference.features.inc';
/**
* Implements hook_menu().
*/
function ting_reference_menu() {
$items = array();
// Callback for the advanced widget to do auto-completion.
$items['ting_reference/autocomplete/object/js'] = array(
'page callback' => '_ting_reference_autocomplete',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_entity_info_alter().
*
* Add new view mode used mainly to display the ting references on node edit
* pages.
*/
function ting_reference_entity_info_alter(&$entity_info) {
// Test that the function exists as it may not during installation, but as
// cache is cleared later on this is not a problem.
if (function_exists('ding_entity_info')) {
$ding_entity_info = ding_entity_info('ding_entity');
// Entity might not be defined yet (is the case in the installer).
if (isset($entity_info[$ding_entity_info['entity_type']]['view modes'])) {
// Add a user_list display mode for add-on modules to use.
$entity_info[$ding_entity_info['entity_type']]['view modes'] += array(
'ting_reference_preview' => array(
'label' => t('Reference preview'),
'custom settings' => TRUE,
),
);
}
}
}
/**
* Implements hook_preprocess_ting_object().
*/
function ting_reference_preprocess_ting_object(&$variables) {
if (isset($variables['content']['ting_reference_reverse'])) {
$variables['content']['ting_reference_reverse']['#prefix'] = '<a name="ting_reference"></a>';
}
}
/**
* Implements hook_field_info().
*/
function ting_reference_field_info() {
return array(
'ting_reference' => array(
'label' => t('Ting reference'),
'description' => t('Stores relationships between Ting objects and Drupal entities.'),
'settings' => array(),
'default_widget' => 'ting_reference_simple',
'default_formatter' => 'ting_reference_default',
),
'ting_reference_reverse' => array(
'label' => t('Ting references'),
'description' => t('Nodes referencing this Ting object.'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_reference_reverse_default',
'no_ui' => TRUE,
),
);
}
/**
* Implements hook_field_is_empty().
*/
function ting_reference_field_is_empty($item, $field) {
return empty($item['ting_object_id']);
}
/**
* Implements hook_field_load().
*/
function ting_reference_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
// This could probably be more efficient if we made a function for
// getting relations for multiple entities at once.
foreach ($entities as $entity_id => $entity) {
switch ($field['type']) {
case 'ting_reference':
case 'ting_reference_reverse':
$relations = ting_reference_get_relations($entity_type, $entity);
$delta = 0;
foreach ($relations as $rid => $relation) {
$items[$entity_id][$delta]['value'] = $relation;
$delta += 1;
}
break;
}
}
}
/**
* Implements hook_field_insert().
*/
function ting_reference_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
$entity_info = entity_get_info($entity_type);
$entity_id = $entity->{$entity_info['entity keys']['id']};
foreach ($items as $item) {
if (!empty($item['tid'])) {
ting_reference_create_relation($entity_type, $entity_id, $item['tid']);
}
}
// Need to clear field cache to display fields properly.
field_cache_clear('field:' . $entity_type . ':' . $entity_id);
}
/**
* Implements hook_field_update().
*/
function ting_reference_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
// Get the entity's id.
$entity_ids = entity_extract_ids($entity_type, $entity);
$entity_id = array_shift($entity_ids);
// Get existing relations.
$relations = ting_reference_get_relations($entity_type, $entity);
// Since there is no way to update the delta value of the endpoints, we need
// to delete all relations, and re-add them.
foreach ($relations as $rid => $relation) {
relation_delete($rid);
}
// Loop through items, create new and mark existing to be kept.
foreach ($items as $item) {
if (!empty($item['tid'])) {
ting_reference_create_relation($entity_type, $entity_id, $item['tid']);
}
}
// Clear cache.
field_cache_clear('field:' . $entity_type . ':' . $entity_id);
}
/**
* Implements hook_field_delete().
*/
function ting_reference_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
// When node is deleted, delete its relations as well.
$relations = ting_reference_get_relations($entity_type, $entity);
foreach ($relations as $rid => $relation) {
relation_delete($rid);
}
}
/**
* Implements hook_field_formatter_info().
*/
function ting_reference_field_formatter_info() {
return array(
'ting_reference_default' => array(
'label' => t('Default'),
'description' => t('Displays the referenced Ting object with a specific display mode.'),
'field types' => array('ting_reference'),
'settings' => array(
'view_mode' => 'teaser',
),
),
'ting_reference_reverse_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_reference_reverse',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function ting_reference_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
switch ($display['type']) {
case 'ting_reference_default':
$entity_info = entity_get_info('ting_object');
$view_modes = array();
foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
$view_modes[$view_mode] = $view_mode_info['label'];
}
$element['view_mode'] = array(
'#title' => t('View mode'),
'#type' => 'select',
'#default_value' => $settings['view_mode'],
'#options' => $view_modes,
);
break;
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function ting_reference_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
switch ($display['type']) {
case 'ting_reference_default':
$view_mode = $settings['view_mode'];
$entity_info = entity_get_info('ting_object');
if (isset($entity_info['view modes'][$view_mode])) {
$view_mode = $entity_info['view modes'][$view_mode]['label'];
}
$summary .= t('View mode @view_mode', array('@view_mode' => $view_mode));
break;
}
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
function ting_reference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'ting_reference_default':
$entity_ids = array();
// Extract the IDs for the ting_object entities we need to load.
foreach ($items as $item) {
$entity_id = $item['value']->endpoints[$langcode][1]['entity_id'];
$entity_ids[$item['value']->rid] = $entity_id;
}
// Load the ting objects found.
$ding_entities = entity_load('ting_object', $entity_ids);
foreach ($ding_entities as $entity) {
$element[] = ting_object_view($entity, $display['settings']['view_mode']);
}
break;
case 'ting_reference_reverse_default':
// Extract the IDs for the entities we need to load.
foreach ($items as $item) {
// Get endpoint information.
$entity_id = $item['value']->endpoints[$langcode][0]['entity_id'];
$entity_type = $item['value']->endpoints[$langcode][0]['entity_type'];
// @TODO: make view mode configurable.
// Load the entity and build it.
$entities = entity_load($entity_type, array($entity_id));
$build = entity_view($entity_type, $entities, 'search_result');
// Don't show links. Should be an option.
unset($build['links']);
$element[] = $build;
}
break;
}
return $element;
}
/**
* Implements hook_field_widget_info().
*/
function ting_reference_field_widget_info() {
return array(
'ting_reference_simple' => array(
'label' => t('Ting reference (simple)'),
'field types' => array('ting_reference'),
'behaviors' => array(
'default value' => FIELD_BEHAVIOR_NONE,
),
),
'ting_reference_advanced' => array(
'label' => t('Ting reference (advanced)'),
'field types' => array('ting_reference'),
),
);
}
/**
* Implements hook_widget_info_alter().
*/
function ting_reference_widget_info_alter(&$info) {
if (isset($info['hidden'])) {
$info['hidden']['field types'][] = 'ting_reference_reverse';
}
}
/**
* Implements hook_field_widget_form().
*/
function ting_reference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// Try to find the relation, if one exists for the current delta.
$ting_object_id = '';
$item = FALSE;
if (!empty($items) && isset($items[$delta]['value'])) {
// Get relations to ting entity.
$item = relation_get_endpoints($items[$delta]['value'], 'ting_object');
$item = array_shift($item['ting_object']);
// Get ting object id.
$ting_object_id = $item->ding_entity_id;
}
// Hidden field used to get the drupal entity id (tid) for a ting_object_id.
$element['tid'] = array(
'#type' => 'hidden',
'#element_validate' => array('ting_reference_object_id_validate'),
);
switch ($instance['widget']['type']) {
case 'ting_reference_simple':
$element['ting_object_id'] = array(
'#type' => 'textfield',
'#description' => t('Enter object ID, FAUST or ISBN number, to select an object from the Ting datawell.'),
'#default_value' => $ting_object_id,
);
break;
case 'ting_reference_advanced':
$element['ting_object_id'] = array(
'#type' => 'textfield',
'#title' => t('Search'),
'#autocomplete_path' => 'ting_reference/autocomplete/object/js',
'#description' => t('Enter a query to search for objects or collections to reference'),
'#default_value' => $ting_object_id,
);
// Load preview of the entity referenced.
if ($item) {
// Get entity type.
$entity_type = $items[$delta]['value']->endpoints[$langcode][1]['entity_type'];
// Load the entity and build it in preview view mode.
$ding_entity = entity_load($entity_type, array($item->tid));
$build = ting_object_view(reset($ding_entity), 'ting_reference_preview');
// Add the build to the form with some css.
$element['preview'] = $build;
$element['#attached']['css'] = array(
drupal_get_path('module', 'ting_reference') . '/css/ting_reference.css',
);
}
break;
}
return $element;
}
/**
* Validate the object ID form element.
*/
function ting_reference_object_id_validate($element, &$form_state, $form) {
// Get the value entered in ting_object_id (the visible element).
$parents = $element['#parents'];
$ting_object_id = $form_state['values'][$parents[0]][$parents[1]][$parents[2]]['ting_object_id'];
// Don't bother validating empty fields.
if (!empty($ting_object_id)) {
module_load_include('client.inc', 'ting');
// If the colon was URL-encoded, decode it - and trim whitespace from
// both ends of the input string.
$ting_object_id = trim(str_replace('%3A', ':', $ting_object_id));
// Load the object to validate it exists.
// Loading it should make it available for use with ting_get_object().
$ting_entity = ting_object_load($ting_object_id);
if ($ting_entity) {
// Set the form values needed.
form_set_value($element, $ting_entity->tid, $form_state);
}
else {
form_set_error(implode('][', $element['#parents']), t('Object “%identifier” could not be found.', array('%identifier' => $ting_object_id)));
}
}
}
/**
* Implements hook_ding_anchor_info().
*/
function ting_reference_ding_anchor_info() {
return array('ting_reference' => t('On this site'));
}
/**
* Utility function to get relations for an entity.
*
* @param string $entity_type
* The entity type.
* @param entity $entity
* The loaded entity.
*
* @return array
* With loaded relation entities.
*/
function ting_reference_get_relations($entity_type, $entity) {
$entity_ids = entity_extract_ids($entity_type, $entity);
$entity_id = array_shift($entity_ids);
// Look up relations, if this entity has an ID. This check will be
// false for a newly created entity that has not been saved to the
// database yet.
if (!empty($entity_id)) {
// For some unknown reason EntityFieldQuery fails and loads both endpoint
// types, resulting in false relations. So we are forced to use a db_select.
$result = db_select('field_data_endpoints', 'endpoints')
->fields('endpoints', array('entity_id'))
->condition('bundle', 'ting_reference')
->condition('endpoints_entity_type', $entity_type)
->condition('endpoints_entity_id', $entity_id)
->orderBy('endpoints.entity_id')
->execute()
->fetchAllAssoc('entity_id');
if ($result) {
return relation_load_multiple(array_keys($result));
}
}
return array();
}
/**
* Utility function to create a relation entity and save it.
*
* @param string $entity_type
* The type of the entity to create the relation from.
* @param string $entity_id
* The id of the entity to create the relation from.
* @param int $tid
* The ting object entity id (Primary key).
*
* @return entity
* The created and saved relation entity.
*/
function ting_reference_create_relation($entity_type, $entity_id, $tid) {
$relation = relation_create('ting_reference', array(
array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'r_index' => 0,
),
array(
'entity_type' => 'ting_object',
'entity_id' => $tid,
'r_index' => 1,
),
));
relation_save($relation);
return $relation;
}
/**
* Auto-complete callback function that searches the data well for objects.
*
* The search is based on the string entered in the UI or the SQL expression.
*
* @param string $string
* The sting to search for or the SQL expression.
*/
function _ting_reference_autocomplete($string) {
$matches = array();
// Load ting client.
module_load_include('client.inc', 'ting');
// Only use auto-complete if more that 3 chars have been written.
if ($string && (strlen($string) > 3)) {
// Try to build the best search query based on the inputted string.
if (preg_match('/(^\d+$)|(^\d+:\d+$)|(=)/', $string)) {
// The query is either ISBN, tid or CQL search expression.
$query = $string;
}
else {
// Add wildcard to the search string.
$query = _ting_search_quote($string) . '*';
}
// Search the data well.
$result = ting_do_search($query, 1, 10, array('facets' => array(), 'allObjects' => FALSE));
// If there is no usable result, exit immediately, providing no reply.
if ($result && $result->numTotalObjects) {
foreach ($result->collections as $collection) {
$object = array_shift($collection->reply->objects);
$matches[$collection->id] = t('!title (!type, !year, !id)', array(
'!title' => isset($object->record['dc:title'][''][0]) ? $object->record['dc:title'][''][0] : '',
'!type' => isset($object->record['dc:type']['dkdcplus:BibDK-Type'][0]) ? $object->record['dc:type']['dkdcplus:BibDK-Type'][0] : '',
'!year' => $object->record['dc:date'][''][0],
'!id' => $object->localId,
));
}
}
else {
$matches[$string] = t('No matches found');
}
}
// Return the result as JSON.
drupal_json_output($matches);
}