profile2_page.module
9.24 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
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
<?php
/**
* @file
* Adds separate pages for viewing and editing profiles.
*/
/**
* Implements hook_menu().
*/
function profile2_page_menu() {
$items = array();
// Bugfix for uninstalling the module, see http://drupal.org/node/1008346.
if (!module_exists('profile2')) {
return;
}
foreach (profile2_get_types() as $type_name => $type) {
if (!empty($type->data['use_page'])) {
$path = profile2_page_get_base_path($type);
$count = count(explode('/', $path));
$items[$path] = array(
'title callback' => 'profile2_page_title',
'title arguments' => array($type_name),
'page callback' => 'profile2_page_own',
'page arguments' => array($path),
'access callback' => 'user_access',
'access arguments' => array("edit own $type_name profile"),
'file' => 'profile2_page.inc',
'menu_name' => 'user-menu',
);
$items[$path . '/%profile2_by_uid'] = array(
'title callback' => 'profile2_page_title',
'title arguments' => array($type_name, $count),
'page callback' => 'profile2_page_view',
'page arguments' => array($count),
'load arguments' => array($type_name),
'access callback' => 'profile2_access',
'access arguments' => array('view', $count),
'file' => 'profile2_page.inc',
// Copied over the following hack from user_menu() to avoid $path
// appearing in the breadcrumb:
//
// By assigning a different menu name, this item (and all registered
// child paths) are no longer considered as children of 'user'. When
// accessing the user account pages, the preferred menu link that is
// used to build the active trail (breadcrumb) will be found in this
// menu (unless there is more specific link), so the link to 'user' will
// not be in the breadcrumb.
'menu_name' => 'navigation',
);
$items[$path . '/%profile2_by_uid/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'load arguments' => array($type_name),
'weight' => -10,
);
$items[$path . '/%profile2_by_uid/edit'] = array(
'page callback' => 'entity_ui_get_form',
'page arguments' => array('profile2', $count),
'load arguments' => array($type_name),
'access callback' => 'profile2_access',
'access arguments' => array('edit', $count),
'title' => 'Edit',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'profile2_page.inc',
);
$items[$path . '/%profile2_by_uid/delete'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('profile2_page_delete_confirm_form', $count),
'load arguments' => array($type_name),
'access callback' => 'profile2_access',
'access arguments' => array('delete', $count),
'title' => 'Delete',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'file' => 'profile2_page.inc',
);
// Devel integration.
if (module_exists('devel')) {
$devel_path = drupal_get_path('module', 'devel');
$items[$path . '/%profile2_by_uid/devel'] = array(
'title' => 'Devel',
'page callback' => 'devel_load_object',
'file' => 'devel.pages.inc',
'file path' => $devel_path,
'page arguments' => array('profile2', $count),
'access arguments' => array('access devel information'),
'type' => MENU_LOCAL_TASK,
'weight' => 100,
);
$items[$path . '/%profile2_by_uid/devel/load'] = array(
'title' => 'Load',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[$path . '/%profile2_by_uid/devel/render'] = array(
'title' => 'Render',
'page callback' => 'devel_render_object',
'page arguments' => array('profile2', $count),
'access arguments' => array('access devel information'),
'file' => 'devel.pages.inc',
'file path' => $devel_path,
'type' => MENU_LOCAL_TASK,
'weight' => 100,
);
}
}
}
return $items;
}
/**
* Menu load callback.
*
* Returns the profile object for the given user. If there is none yet, a new
* object is created.
*/
function profile2_by_uid_load($uid, $type_name) {
if ($uid && is_numeric($uid) && ($account = user_load($uid))) {
$profile = profile2_load_by_user($account, $type_name);
if (!$profile) {
$profile = profile2_create(array('type' => $type_name));
$profile->setUser($account);
$profile->is_new = TRUE;
}
return $profile;
}
return FALSE;
}
/**
* Returns the base path to use as profile page.
*/
function profile2_page_get_base_path($profile_type) {
// Allow for an easy customization of the page's base path.
if (!empty($profile_type->data['page_path'])) {
return $profile_type->data['page_path'];
}
return 'profile-' . $profile_type->type;
}
/**
* Implements hook_forms().
*/
function profile2_page_forms($form_id, $args) {
// For efficiency, only act if the third argument is 'profile2'.
if (isset($args[2]) && is_string($args[2]) && $args[2] == 'profile2') {
$info = entity_get_info('profile2');
// Translate bundle form ids to the base form id 'profile2_form'.
foreach ($info['bundles'] as $bundle => $bundle_info) {
$forms['profile2_edit_' . $bundle . '_form']['callback'] = 'profile2_form';
$forms['profile2_edit_' . $bundle . '_form']['wrapper callback'] = 'entity_ui_form_defaults';
}
return $forms;
}
}
/**
* Implements hook_profile2_type_load().
*/
function profile2_page_profile2_type_load($types) {
foreach ($types as $type) {
if (!empty($type->data['use_page'])) {
// Disable user categories and the user account view.
$type->userCategory = FALSE;
$type->userView = FALSE;
}
}
}
/**
* Implements hook_entity_info_alter().
*/
function profile2_page_entity_info_alter(&$entity_info) {
// Add new view modes for the page.
$entity_info['profile2']['view modes']['page'] = array(
'label' => t('Profile page'),
'custom settings' => FALSE,
);
$entity_info['profile2']['view modes']['teaser'] = array(
'label' => t('Teaser'),
'custom settings' => FALSE,
);
$entity_info['profile2']['uri callback'] = 'profile2_page_uri_callback';
$entity_info['profile2']['form callback'] = 'profile2_page_form_callback';
}
/**
* URI callback pointing to the profile page.
*
* @see profile2_pages_entity_info_alter()
*/
function profile2_page_uri_callback($profile) {
$type = $profile->type();
if (!empty($type->data['use_page'])) {
return array('path' => profile2_page_get_base_path($type) . '/' . $profile->uid);
}
// Fall back to the default callback.
return $profile->defaultUri();
}
/**
* Form callback for entity_form().
*/
function profile2_page_form_callback($profile) {
// Pre-populate the form-state with the right form include.
$form_state = form_state_defaults();
form_load_include($form_state, 'inc', 'profile2_page');
return entity_ui_get_form('profile2', $profile, 'edit', $form_state);
}
/**
* Menu title callback.
*/
function profile2_page_title($type_name, $profile2 = NULL) {
$type = profile2_get_types($type_name);
// If no profile is given, we are at the general path pointing to the own
// profile.
if (!isset($profile2)) {
return t('My @profile-label', array('@profile-label' => drupal_strtolower($type->getTranslation('label'))));
}
return drupal_ucfirst($profile2->label());
}
/**
* Implements hook_form_FORM_ID_alter() for the profile2 type form..
*/
function profile2_page_form_profile2_type_form_alter(&$form, &$form_state) {
$type = $form_state['profile2_type'];
$form['data']['use_page'] = array(
'#type' => 'checkbox',
'#title' => t('Provide a separate page for editing profiles.'),
'#description' => t('If enabled, a separate menu item for editing the profile is generated and the profile is hidden from the user account page.'),
'#default_value' => !empty($type->is_new) || !empty($type->data['use_page']),
);
$form['data']['#tree'] = TRUE;
}
/**
* Implements hook_profile2_type_insert().
*/
function profile2_page_profile2_type_insert(ProfileType $type) {
// Do not directly issue menu rebuilds here to avoid potentially multiple
// rebuilds. Instead, let menu_get_item() issue the rebuild on the next page.
if (!empty($type->data['use_page'])) {
variable_set('menu_rebuild_needed', TRUE);
}
}
/**
* Implements hook_profile2_type_update().
*/
function profile2_page_profile2_type_update(ProfileType $type) {
// Rebuild the menu if use_page or the type name has been changed.
// @see profile2_page_profile2_type_insert()
if (empty($type->data['use_page']) != empty($type->original->data['use_page']) || ($type->data['use_page'] && $type->type != $type->original->type)) {
variable_set('menu_rebuild_needed', TRUE);
}
}
/**
* Implements hook_profile2_type_delete()
*/
function profile2_page_profile2_type_delete($type) {
// Do not directly issue menu rebuilds here to avoid potentially multiple
// rebuilds. Instead, let menu_get_item() issue the rebuild on the next page.
if (!empty($type->data['use_page'])) {
variable_set('menu_rebuild_needed', TRUE);
}
}