forked from ding2/ting_material_details
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathting_material_details.field.inc
459 lines (436 loc) · 13.5 KB
/
ting_material_details.field.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
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
<?php
/**
* Implements hook_field_info()
*/
function ting_material_details_field_info() {
$ret = array(
//dc:type
'ting_details_type' => array(
'label' => t('Type'),
'description' => t('Material type'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dc:format
'ting_details_format' => array(
'label' => t('Format'),
'description' => t('Material format'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dcterms:isPartOf (Availability)
'ting_details_ispartof' => array(
'label' => t('Available in'),
'description' => t('dcterms:isPartOf'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dc:language
'ting_details_language' => array(
'label' => t('Language'),
'description' => t('Language'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dkdcplus:spoken
'ting_details_spoken' => array(
'label' => t('Speech'),
'description' => t('spoken language'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dkdcplus:subtitles
'ting_details_subtitles' => array(
'label' => t('Subtitle'),
'description' => t('Material subtitles'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dkdcplus:genre
'ting_details_genre' => array(
'label' => t('Genre'),
'description' => t('Material genre'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dc:subjects
'ting_details_subjects' => array(
'label' => t('Subject'),
'description' => t('Material subject'),
'default_widget' => 'hidden',
// 'default_formatter' => 'ting_details_single',
'default_formatter' => 'ting_details_subject_search',
'no_ui' => TRUE,
),
// Classification (dc:subject->dkdcplus:DK5)
'ting_details_classification' => array(
'label' => t('Classification'),
'description' => t('Material classification'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
//dcterms:spatial ??
'ting_details_spatial' => array(
'label' => t('spatial'),
'description' => t('Material spatial'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
//dc:contributor
'ting_details_contributor' => array(
'label' => t('Reader'),
'description' => t('Material contributor'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_creator_search',
'no_ui' => TRUE,
),
//dkdcplus:mus (musician)
'ting_details_musician' => array(
'label' => t('Musician'),
'description' => t('Material musician'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
//dkdcplus:track
'ting_details_tracks' => array(
'label' => t('Contains'),
'description' => t('Material tracks'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
//dcterms:isReferencedBy
'ting_details_referenced' => array(
'label' => t('Referenced by'),
'description' => t('Material referenced'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dc:description
'ting_details_description' => array(
'label' => t('Description'),
'description' => t('Material description'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
//dc:source (original title)
'ting_details_source' => array(
'label' => t('Original title'),
'description' => t('Material original title'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dcterms:replaces
'ting_details_replaces' => array(
'label' => t('Previous title'),
'description' => t('Material previous title'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dcterms:isReplacedBy
'ting_details_replaced_by' => array(
'label' => t('Later title'),
'description' => t('Material later title'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dkdcplus:ISBN
'ting_details_isbn' => array(
'label' => t('ISBN no.'),
'description' => t('Material ISBN'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dcterms:URI
'ting_details_uri' => array(
'label' => t('Host publication'),
'description' => t('Material URI'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dkdcplus:version
'ting_details_version' => array(
'label' => t('Version'),
'description' => t('Material version'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dcterms:extent
'ting_details_extent' => array(
'label' => t('Extent'),
'description' => t('Material extent'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dc:publisher
'ting_details_publisher' => array(
'label' => t('Publisher'),
'description' => t('Material publisher'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dc:rights
'ting_details_rights' => array(
'label' => t('Rights'),
'description' => t('Material rights'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
// dcterms:audience
'ting_details_audience' =>array(
'label' => t('Audience'),
'description' => t('Material audience'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
//dkdcplus:age
'ting_details_age' =>array(
'label' => t('Age'),
'description' => t('Material age'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
//dkdcplus:pegi
'ting_details_pegi' =>array(
'label' => t('Pegi'),
'description' => t('Material PEGI-mark'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_details_single',
'no_ui' => TRUE,
),
);
return $ret;
}
/**
* Implements hook_field_formatter_info
*
* Notes; 'field types' are passed on to hook_field_formatter_view
*/
function ting_material_details_field_formatter_info() {
return array(
'ting_details_single' => array(
'label' => t('Default'),
'field types' => array(
'ting_details_type',
'ting_details_format',
'ting_details_ispartof',
'ting_details_language',
'ting_details_spoken',
'ting_details_subtitles',
'ting_details_classification',
'ting_details_spatial',
'ting_details_musician',
'ting_details_referenced',
'ting_details_description',
'ting_details_source',
'ting_details_replaces',
'ting_details_replaced_by',
'ting_details_isbn',
'ting_details_uri',
'ting_details_version',
'ting_details_extent',
'ting_details_publisher',
'ting_details_rights',
'ting_details_age',
'ting_details_audience',
'ting_details_subjects',
'ting_details_tracks',
'ting_details_genre',
'ting_details_contributor',
'ting_details_pegi',
),
),
'ting_details_creator_search' => array(
'label' => t('Creator search link'),
'field types' => array(
'ting_details_contributor',
),
),
'ting_details_subject_search' => array(
'label' => t('Subject search link'),
'field types' => array(
'ting_details_subjects',
),
),
);
}
/*
* helper function for retrieving record-field,
* maps field name to record structure
*/
function ting_material_details_record_info() {
$ret = array();
$ret['ting_details_type'] = 'type';
$ret['ting_details_format'] = array('dc:format');
$ret['ting_details_ispartof'] = array('dcterms:isPartOf');
$ret['ting_details_language'] = 'language';
$ret['ting_details_spoken'] = array('dc:language'=>'dkdcplus:spoken');
$ret['ting_details_subtitles'] = array('dc:language'=>'dkdcplus:subtitles');
$ret['ting_details_genre'] = array('dc:subject'=>'dkdcplus:genre');
$ret['ting_details_subjects'] = 'subjects';
$ret['ting_details_classification'] = 'classification'; //array('dc:subject'=>'dkdcplus:DK5-Text');
$ret['ting_details_spatial'] = array('dcterms:spatial');
$ret['ting_details_contributor'] = 'contributors'; //array('dc:contributor'=>'dkdcplus:cmp');
$ret['ting_details_musician'] = array('dc:contributor'=>'dkdcplus:mus');
$ret['ting_details_tracks'] = array('dcterms:hasPart'=>'dkdcplus:track');
$ret['ting_details_referenced'] = array('dcterms:isReferencedBy');
$ret['ting_details_description'] = array('dc:description'=>'dkdcplus:series');
$ret['ting_details_source'] = array('dc:source');
$ret['ting_details_replaces'] = array('dcterms:replaces');
$ret['ting_details_replaced_by'] = array('dcterms:replacedBy');
$ret['ting_details_isbn'] = array('dc:identifier'=>'dkdcplus:ISBN');
$ret['ting_details_uri'] = array('dc:identifier'=>'dcterms:URI');
$ret['ting_details_version'] = array('dkdcplus:version');
$ret['ting_details_extent'] = array('dcterms:extent');
$ret['ting_details_publisher'] = array('dc:publisher');
$ret['ting_details_rights'] = array('dc:rights');
$ret['ting_details_age'] = array('dcterms:audience'=>'dkdcplus:age');
$ret['ting_details_audience'] = array('dcterms:audience');
$ret['ting_details_pegi'] = array('dcterms:audience'=>'dkdcplus:pegi');
return $ret;
}
/*
* helper function
* return array
*/
function ting_material_details_get_record_value( $field_type, $entity ) {
$rec_info = ting_material_details_record_info();
$rec = isset($rec_info[$field_type]) ? $rec_info[$field_type] : FALSE;
if( !$rec ) {
return FALSE;
}
if( is_string($rec) ) {
$value = $entity->$rec ;
}
if( is_array($rec) ) {
foreach( $rec as $key => $val ) {
if( $key ) {
$value = isset($entity->record[$key][$val]) ? $entity->record[$key][$val] : FALSE;
}
else {
$value = isset( $entity->record[$val][''] ) ? $entity->record[$val][''] : FALSE;
}
}
}
return ting_material_details_parse( $field_type,$value );
}
/*
* helper function
* handle special cases
*/
function ting_material_details_parse($field_type, $value) {
if(!$value) {
return FALSE;
}
switch($field_type) {
case 'ting_details_uri':
foreach ($value as $key => $val) {
$ret[] = l($val, $val);
}
break;
default :
$ret = $value;
}
return $ret;
}
/**
* Implements hook_field_formatter_view()
*/
function ting_material_details_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $items) {
if ($val = ting_material_details_get_record_value($field['type'], $entity)) {
switch ($display['type']) {
case 'ting_details_single':
$element[$delta] = array(
'#markup' => is_array($val) ? implode(', ', $val) : $val,
);
break;
case 'ting_details_creator_search':
if (!is_array($val)) {
$val = array($val);
}
$links = array();
foreach ($val as $value) {
$links[] = l($value, 'search/ting/dc.creator=' . $value);
}
$element[$delta] = array(
'#markup' => implode(', ', $links),
);
break;
case 'ting_details_subject_search' :
if( !is_array($val) ) {
$val = array($val);
}
$links = array();
foreach($val as $value) {
$links[] = l($value, 'search/ting/dc.subject=' . $value);
}
$element[$delta] = array(
'#markup' => implode(', ', $links),
);
break;
}
}
}
return $element;
}
/**
* Implements hook_field_validate
* TODO is some kind of validation needed?
*/
function ting_material_details_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
// do nothing
}
/**
* Implements hook_field_is_empty
* TODO; check
*/
function ting_material_details_is_empty($item, $field) {
return false;
}
/**
* Implements hook_field_load().
* Notes: Define custom load behavior for this module's field types.
*/
function ting_material_details_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $id => $entity) {
$items[$id][0] = array(
'id' => $id,
);
}
}
/**
* Implements hook_field_formatter_prepare_view
* we could also use hook_field_load or hook_field_prepare_view
*/
function ting_material_details_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
foreach ($entities as $id => $entity) {
$items[$id][0] = array(
'id' => $id,
);
}
}