-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathog_serial.module
More file actions
202 lines (182 loc) · 4.76 KB
/
og_serial.module
File metadata and controls
202 lines (182 loc) · 4.76 KB
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
<?php
/**
* @file
* Drupal hook module system.
*/
include_once 'og_serial.inc';
/**
* OG Serial default field name.
*/
define('OG_SERIAL_FIELD', 'og_serial');
/**
* Implements hook_menu().
*/
function og_serial_menu() {
$items = array();
$items['group/%/%/admin/serial'] = array(
'title' => 'Serial',
'description' => 'Manage group content serial number.',
'page callback' => 'og_serial_overview_page',
'file' => 'og_serial.admin.inc',
'access callback' => TRUE,
);
return $items;
}
/**
* Implements hook_og_ui_get_group_admin()
*/
function og_serial_og_ui_get_group_admin($group_type, $gid) {
$items = array();
if (og_user_access($group_type, $gid, 'administer serial')) {
$items['serial'] = array(
'title' => t('Serial'),
'description' => t('Show serial counter related to this group.'),
'href' => 'admin/serial',
);
}
return $items;
}
/**
* Implements hook_og_permissions().
*/
function og_serial_og_permission() {
$permissions = array(
'administer serial' => array(
'title' => t('Administer group content serial number counter.'),
),
);
return $permissions;
}
/**
* Implements hook_og_fields_info().
*/
function og_serial_og_fields_info() {
$items[OG_SERIAL_FIELD] = array(
'type' => array('group content'),
'description' => t('Serial number of group content related to accessible groups.'),
'field' => array(
'settings' => array(),
'field_name' => OG_SERIAL_FIELD,
'type' => 'og_serial',
'cardinality' => 1,
),
'instance' => array(
'label' => t('OG serial'),
'widget' => array(
'module' => 'og_serial',
'settings' => array(),
'type' => 'og_serial_hidden',
),
'display' => array(
'default' => array(
'type' => 'og_serial_default',
),
),
),
);
return $items;
}
/**
* Implements hook_field_info().
*/
function og_serial_field_info() {
return array(
'og_serial' => array(
'label' => t('OG Serial'),
'description' => t('Serial number for group content related to a group.'),
'default_widget' => 'og_serial_widget',
'default_formatter' => 'og_serial_formatter',
),
);
}
/**
* Implements hook_field_widget_info().
*/
function og_serial_field_widget_info() {
return array(
'og_serial_hidden' => array(
'label' => t('Hidden'),
'field types' => array('og_serial'),
'settings' => array(),
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function og_serial_field_formatter_info() {
return array(
'og_serial_default' => array(
'label' => t('OG serial default'),
'field types' => array('og_serial'),
'settings' => array(),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function og_serial_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
case 'og_serial_default':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#markup' => theme('og_serial_formatter_default', $item),
);
}
break;
}
return $element;
}
/**
* Implements hook_field_is_empty().
*/
function og_serial_field_is_empty($item, $field) {
if (!empty($item['ogs_id'])) {
return FALSE;
}
return TRUE;
}
/**
* Implements hook_theme().
*/
function og_serial_theme() {
// Register the theme for the default formatter.
return array(
'og_serial_formatter_default' => array(
'variables' => array(
'ogs_id' => NULL,
'sid' => NULL,
),
),
);
}
// -----------------------------------------------------------------------------
// Correctly insert/update og_serial field in many situations.
// Best practice: Do not change the group after created, current mechanism is
// abandon the old serial id and assign new, so if you don't mind the id gap.
/**
* @implements hook_og_membership_presave().
*/
function og_serial_og_membership_presave(OgMembership $og_membership) {
// Being insert.
// Group is being changed.
if ($og_membership->is_new) {
$entity = $og_membership->entity;
$entity_type = $og_membership->entity_type;
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
$group_type = $og_membership->group_type;
$gid = $og_membership->gid;
if (og_serial_ogs_is_installed($group_type, $gid, $entity_type, $bundle)) {
$ogs_id = og_serial_ogs_get_id($group_type, $gid, $entity_type, $bundle);
$language = entity_language($entity_type, $entity);
$entity->og_serial[$language][0] = array(
'ogs_id' => $ogs_id,
'sid' => og_serial_ogs_get_counter($ogs_id) + 1,
);
og_serial_ogs_increase_counter($ogs_id);
}
}
}