forked from ding2/ting
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathting.field.inc
407 lines (388 loc) · 11.9 KB
/
ting.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
<?php
/**
* @file
* Field hook implementations.
*/
/**
* Implements hook_field_info().
*/
function ting_field_info() {
return array(
// ting_object
'ting_title' => array(
'label' => t('Ting object title'),
'description' => t('Ting object title'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_title_default',
'no_ui' => TRUE,
),
'ting_type' => array(
'label' => t('Ting object type'),
'description' => t('Ting object type'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_type_default',
'no_ui' => TRUE,
),
'ting_series' => array(
'label' => t('Ting object series'),
'description' => t('Ting object series'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_series_default',
'no_ui' => TRUE,
),
'ting_abstract' => array(
'label' => t('Ting object abstract'),
'description' => t('Ting object abstract'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_abstract_default',
'no_ui' => TRUE,
),
'ting_author' => array(
'label' => t('Ting object author'),
'description' => t('Ting object author'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_author_default',
'no_ui' => TRUE,
),
'ting_subjects' => array(
'label' => t('Ting object subjects'),
'description' => t('Ting object subjects'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_subjects_default',
'no_ui' => TRUE,
),
// ting_collection
'ting_primary_object' => array(
'label' => t('Ting collection primary object'),
'description' => t('Ting collection primary object'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_primary_object_default',
'no_ui' => TRUE,
),
'ting_entities' => array(
'label' => t('Ting collection listing'),
'description' => t('Ting collection listing'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_entities_default',
'no_ui' => TRUE,
),
'ting_collection_types' => array(
'label' => t('Ting collection object types'),
'description' => t('Ting collection object types'),
'default_widget' => 'hidden',
'default_formatter' => 'ting_collection_types_default',
'no_ui' => TRUE,
),
);
}
/**
* Implements hook_widget_info_alter().
*/
function ting_widget_info_alter(&$info) {
if (isset($info['hidden'])) {
$info['hidden']['field types'] += array(
'ting_title',
'ting_type',
'ting_abstract',
'ting_author',
'ting_subjects',
'ting_primary_object',
'ting_entities',
'ting_collection_types',
);
}
}
/**
* Implements hook_field_load().
*/
function ting_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
// Transform entities into a multiple field.
if ($field['type'] == 'ting_entities') {
foreach ($entities as $id => $entity) {
foreach ($entity->entities as $ent) {
$items[$id][] = array(
'id' => $ent->id,
);
}
}
}
else {
foreach ($entities as $id => $entity) {
$items[$id][0] = array(
'id' => $entity->id,
);
}
}
}
/**
* Implements hook_field_formatter_info().
*/
function ting_field_formatter_info() {
return array(
'ting_title_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_title',
),
'settings' => array(
'link_type' => 'none',
),
),
'ting_type_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_type',
),
),
'ting_series_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_series',
),
),
'ting_abstract_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_abstract',
),
),
'ting_author_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_author',
),
),
'ting_subjects_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_subjects',
),
),
'ting_primary_object_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_primary_object',
),
'settings' => array(
'view_mode' => 'teaser',
),
),
'ting_entities_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_entities',
),
'settings' => array(
'hide_primary' => TRUE,
'view_mode' => 'teaser',
),
),
'ting_collection_types_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_collection_types',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function ting_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = NULL;
switch ($field['type']) {
case 'ting_title':
$element['link_type'] = array(
'#type' => 'radios',
'#title' => t('Link title to'),
'#options' => array(
'none' => t("Don't link"),
'object' => t('Object'),
'collection' => t('Collection, if part of a collection'),
),
'#default_value' => $settings['link_type'],
);
break;
case 'ting_entities':
$element['hide_primary'] = array(
'#type' => 'checkbox',
'#title' => t('Hide primary'),
'#description' => t("Don't show the primary object as part of the list."),
'#default_value' => $settings['hide_primary'],
);
case 'ting_primary_object':
$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_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
switch ($field['type']) {
case 'ting_title':
$summary .= t('Link type: @type', array('@type' => $settings['link_type']));
break;
case 'ting_entities':
$summary .= $settings['hide_primary'] ? t("Don't show primary. ") : t("Include primary. ");
case 'ting_primary_object':
$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_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// Entity listing is a special case.
if ($display['type'] == 'ting_entities_default') {
$entities = $entity->entities;
// Skip the first.
if ($display['settings']['hide_primary']) {
$primary = array_shift($entities);
}
foreach ($entity->types as $type) {
$element[0][$type] = array(
'#prefix' => '<h2>' . $type . '</h2><a name="' . $type . '"></a>',
);
}
foreach ($entities as $sub_entity) {
$element[0][$sub_entity->type][$sub_entity->id] = ting_object_view($sub_entity, $display['settings']['view_mode']);
}
return $element;
}
foreach ($items as $delta => $item) {
switch ($display['type']) {
case 'ting_title_default':
/*
* This doesn't work due to the way objects gets cached at the
* moment. Until that is fixed, link all objects to collections when
* requested, the page will redirect to ting/object/X if there is only
* one.
*/
$type = 'ting_object';
if ($display['settings']['link_type'] == 'collection') {
// // Check if the cache contains an collection for this id.
// $x = ting_cache_get($entity->id);
// if ($x instanceof TingClientObjectCollection) {
$type = 'ting_collection';
// }
}
if ($display['settings']['link_type'] != 'none') {
$url = entity_uri($type, $entity);
$title = l($entity->title, $url['path'], $url['options']);
}
else {
$title = check_plain($entity->title);
}
$element[$delta] = array(
'#prefix' => '<h2>',
'#suffix' => '</h2>',
'#markup' => $title,
);
break;
case 'ting_type_default':
$element[$delta] = array(
'#theme' => 'item_list',
'#items' => array(
array(
'data' => $entity->type,
'class' => array(drupal_html_class($entity->type)),
)
),
);
break;
case 'ting_series_default':
if ($entity->serieTitle) {
$element[$delta] = array(
// should use phrase.title instead of dc.title but at the moment opensearch does not support the use of phrase.title
'#markup' => l(check_plain($entity->serieTitle), 'search/ting/' . variable_get('ting_admin_register_serie_title', 'dc.title') . '=' . drupal_strtolower($entity->serieTitle), array('attributes' => array('class' => array('series')))),
);
}
break;
case 'ting_abstract_default':
$element[$delta] = array(
'#markup' => check_plain($entity->abstract),
);
break;
case 'ting_author_default':
$creators = array();
foreach ($entity->creators as $i => $creator) {
$creators[] = l($creator, 'search/ting/dc.creator=' . $creator, array('attributes' => array('class' => array('author'))));
}
$markup_string = '';
if (count($creators)) {
if ($entity->date != '') {
$markup_string = t('By !author_link (@year)', array(
'!author_link' => implode(', ', $creators),
'@year' => $entity->date, // So wrong, but appears to
// be the way the data is.
));
}
else {
$markup_string = t('By !author_link', array(
'!author_link' => implode(', ', $creators),
));
}
}
elseif($entity->date != '') {
$markup_string = t('(@year)', array('@year'=>$entity->date));
}
$element[$delta] = array(
'#markup' => $markup_string,
);
break;
case 'ting_subjects_default':
if (count($entity->subjects) == TRUE) {
$subjects = array();
foreach ($entity->subjects as $subject) {
$subjects[] = l($subject, 'search/ting/dc.subject=' . $subject, array('attributes' => array('class' => array('subject'))));
}
$element[$delta] = array(
'#markup' => implode(' ', $subjects),
);
}
break;
case 'ting_primary_object_default':
$element[$delta] = ting_object_view($entity->primary_object, $display['settings']['view_mode']);
break;
case 'ting_collection_types_default':
$types = array();
foreach ($entity->types as $type) {
$types[] = array(
'data' => $type,
'class' => array(drupal_html_class($type)),
);
}
$element[$delta] = array(
'#theme' => 'item_list',
'#items' => $types,
);
break;
}
}
return $element;
}