-
Notifications
You must be signed in to change notification settings - Fork 7
/
ding_periodical.field.inc
136 lines (124 loc) · 3.61 KB
/
ding_periodical.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
<?php
/**
* @file
* Field hook implementations.
*/
/**
* Implements hook_field_info().
*/
function ding_periodical_field_info() {
return array(
'ding_periodical_issues' => array(
'label' => t('Issue list for periodicals.'),
'description' => t('Issue list for periodicals.'),
'default_widget' => 'hidden',
'default_formatter' => 'ding_periodical_issues_default',
'no_ui' => TRUE,
),
);
}
/**
* Implements hook_field_load().
*/
function ding_periodical_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $id => $entity) {
// Only bother with entities that are periodicals. This is cached, but
// entities are unlikely to change type.
if ($entity->is('periodical')) {
$items[$id][0] = array(
'provider_id' => $entity->localId,
);
}
}
}
/**
* Implements hook_widget_info_alter().
*/
function ding_periodical_widget_info_alter(&$info) {
if (isset($info['hidden'])) {
$info['hidden']['field types'][] = 'ding_periodical_issues';
}
}
/**
* Implements hook_field_formatter_info().
*/
function ding_periodical_field_formatter_info() {
return array(
'ding_periodical_issues_default' => array(
'label' => t('Default'),
'field types' => array(
'ding_periodical_issues',
),
),
'ding_periodical_issues_ajax' => array(
'label' => t('Ajax load'),
'field types' => array(
'ding_periodical_issues',
),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function ding_periodical_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// Check that the provider provides holdings information.
if (!ding_provider_implements('availability', 'holdings')) {
return $element;
}
// Switch over the different formatters.
switch ($display['type']) {
case 'ding_periodical_issues_ajax':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#theme' => 'ding_periodical_issues_ajax',
'#ding_entity_id' => $entity->ding_entity_id,
'#attached' => array(
'js' => array(
array(
'data' => drupal_get_path('module', 'ding_periodical') . '/js/ding_periodical_ajax.js',
),
array(
'data' => array(
'ding_periodical' => array(
'id' => drupal_html_class($entity->ding_entity_id),
'ding_entity_id' => $entity->ding_entity_id,
),
),
'type' => 'setting',
),
),
),
);
}
break;
default:
// Handles the default formatter.
foreach ($items as $delta => $item) {
$availability = ding_provider_invoke('availability', 'holdings', array($entity->provider_id));
if (isset($availability[$entity->provider_id])) {
$issues = $availability[$entity->provider_id]['issues'];
krsort($issues);
if ($issues) {
$element[$delta] = array(
'#theme' => 'ding_periodical_issues',
'#entity' => $entity,
'#issues' => $issues,
'#availability' => $availability,
);
}
}
}
break;
}
// Add script to collapsed the issue list, if any elements found.
if (!empty($element)) {
$element['#attached']['js'] = array(
array(
'data' => drupal_get_path('module', 'ding_periodical') . '/js/ding_periodical.js',
),
);
}
return $element;
}