forked from backdrop-contrib/profile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.api.php
321 lines (301 loc) · 8.34 KB
/
profile.api.php
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
<?php
/**
* @file
* This file contains no working PHP code; it exists to provide additional
* documentation for doxygen as well as to document hooks in the standard
* Backdrop manner.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Act on profiles being loaded from the database.
*
* This hook is invoked during profile loading, which is handled by
* entity_load(), via the EntityCRUDController.
*
* @param $entities
* An array of profile entities being loaded, keyed by id.
*
* @see hook_entity_load()
*/
function hook_profile_load($entities) {
$result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys($entities)));
foreach ($result as $record) {
$entities[$record->pid]->foo = $record->foo;
}
}
/**
* Respond when a profile is inserted.
*
* This hook is invoked after the profile is inserted into the database.
*
* @param profile
* The profile that is being inserted.
*
* @see hook_entity_insert()
*/
function hook_profile_insert($profile) {
db_insert('mytable')
->fields(array(
'pid' => $profile->pid,
'extra' => $profile->extra,
))
->execute();
}
/**
* Act on a profile being inserted or updated.
*
* This hook is invoked before the profile is saved to the database.
*
* @param $profile
* The profile that is being inserted or updated.
*
* @see hook_entity_presave()
*/
function hook_profile_presave($profile) {
$profile->extra = 'foo';
}
/**
* Respond to a profile being updated.
*
* This hook is invoked after the profile has been updated in the database.
*
* @param $profile
* The $profile that is being updated.
*
* @see hook_entity_update()
*/
function hook_profile_update($profile) {
db_update('mytable')
->fields(array('extra' => $profile->extra))
->condition('pid', $profile->pid)
->execute();
}
/**
* Respond to profile deletion.
*
* This hook is invoked after the profile has been removed from the database.
*
* @param $profile
* The profile that is being deleted.
*
* @see hook_entity_delete()
*/
function hook_profile_delete($profile) {
db_delete('mytable')
->condition('pid', $profile->pid)
->execute();
}
/**
* Act on a profile that is being assembled before rendering.
*
* @param $profile
* The profile entity.
* @param $view_mode
* The view mode the profile is rendered in.
* @param $langcode
* The language code used for rendering.
*
* The module may add elements to $profile->content prior to rendering. The
* structure of $profile->content is a renderable array as expected by
* backdrop_render().
*
* @see hook_entity_prepare_view()
* @see hook_entity_view()
*/
function hook_profile_view($profile, $view_mode, $langcode) {
$profile->content['my_additional_field'] = array(
'#markup' => $additional_field,
'#weight' => 10,
'#theme' => 'mymodule_my_additional_field',
);
}
/**
* Alter the results of entity_view() for profiles.
*
* @param $build
* A renderable array representing the profile content.
*
* This hook is called after the content has been assembled in a structured
* array and may be used for doing processing which requires that the complete
* profile content structure has been built.
*
* If the module wishes to act on the rendered HTML of the profile rather than
* the structured content array, it may use this hook to add a #post_render
* callback. Alternatively, it could also implement hook_preprocess_profile().
* See backdrop_render() and theme() documentation respectively for details.
*
* @see hook_entity_view_alter()
*/
function hook_profile_view_alter($build) {
if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
// Change its weight.
$build['an_additional_field']['#weight'] = -10;
// Add a #post_render callback to act on the rendered HTML of the entity.
$build['#post_render'][] = 'my_module_post_render';
}
}
/**
* Act on profile type being loaded from the database.
*
* This hook is invoked during profile type loading, which is handled by
* entity_load(), via the EntityCRUDController.
*
* @param $types
* An array of profiles being loaded, keyed by profile type names.
*/
function hook_profile_type_load($types) {
if (isset($types['main'])) {
$types['main']->userview = FALSE;
}
}
/**
* Respond when a profile type is inserted.
*
* This hook is invoked after the profile type is inserted into the database.
*
* @param $type
* The profile type that is being inserted.
*/
function hook_profile_type_insert($type) {
db_insert('mytable')
->fields(array(
'id' => $type->id,
'extra' => $type->extra,
))
->execute();
}
/**
* Act on a profile type being inserted or updated.
*
* This hook is invoked before the profile type is saved to the database.
*
* @param $type
* The profile type that is being inserted or updated.
*/
function hook_profile_type_presave($type) {
$type->extra = 'foo';
}
/**
* Respond to updates to a profile.
*
* This hook is invoked after the profile type has been updated in the database.
*
* @param $type
* The profile type that is being updated.
*/
function hook_profile_type_update($type) {
db_update('mytable')
->fields(array('extra' => $type->extra))
->condition('id', $type->id)
->execute();
}
/**
* Respond to profile type deletion.
*
* This hook is invoked after the profile type has been removed from the
* database.
*
* @param $type
* The profile type that is being deleted.
*/
function hook_profile_type_delete($type) {
db_delete('mytable')
->condition('id', $type->id)
->execute();
}
/**
* Define default profile type configurations.
*
* @return
* An array of default profile types, keyed by profile type names.
*/
function hook_default_profile_type() {
$types['main'] = new ProfileType(array(
'type' => 'main',
'label' => t('Profile'),
'weight' => 0,
'locked' => TRUE,
));
return $types;
}
/**
* Alter default profile type configurations.
*
* @param $defaults
* An array of default profile types, keyed by type names.
*
* @see hook_default_profile_type()
*/
function hook_default_profile_type_alter(&$defaults) {
$defaults['main']->label = 'custom label';
}
/**
* Alter profile forms.
*
* Modules may alter the profile entity form regardless to which form it is
* attached by making use of this hook or the profile type specifiy
* hook_form_profile_edit_PROFILE_TYPE_form_alter(). #entity_builders may be
* used in order to copy the values of added form elements to the entity, just
* as described by entity_form_submit_build_entity().
*
* @param $form
* Nested array of form elements that comprise the form.
* @param $form_state
* A keyed array containing the current state of the form.
*
* @see profile_attach_form()
*/
function hook_form_profile_form_alter(&$form, &$form_state) {
// Your alterations.
}
/**
* Control access to profiles.
*
* Modules may implement this hook if they want to have a say in whether or not
* a given user has access to perform a given operation on a profile.
*
* @param $op
* The operation being performed. One of 'view', 'edit' (being the same as
* 'create' or 'update') and 'delete'.
* @param $profile
* (optional) A profile to check access for. If nothing is given, access for
* all profiles is determined.
* @param $account
* (optional) The user to check for. If no account is passed, access is
* determined for the global user.
* @return boolean
* Return TRUE to grant access, FALSE to explicitly deny access. Return NULL
* or nothing to not affect the operation.
* Access is granted as soon as a module grants access and no one denies
* access. Thus if no module explicitly grants access, access will be denied.
*
* @see profile_access()
*/
function hook_profile_access($op, $profile = NULL, $account = NULL) {
if (isset($profile)) {
// Explicitly deny access for a 'secret' profile type.
if ($profile->type == 'secret' && !user_access('custom permission')) {
return FALSE;
}
// For profiles other than the default profile grant access.
if ($profile->type != 'main' && user_access('custom permission')) {
return TRUE;
}
// In other cases do not alter access.
}
}
/**
* Alter the operation dropdown button in the Profile Type table.
*/
function hook__profile_get_operations_alter(&$links, $id) {
$links['translate'] = array(
'title' => t('Translate'),
'href' => 'admin/structure/profiles/manage/' . $id . '/translate',
);
}
/**
* @}
*/