5 * Support for configurable user profiles.
9 * Private field, content only available to privileged users.
11 define('PROFILE_PRIVATE', 1);
14 * Public field, content shown on profile page but not used on member list pages.
16 define('PROFILE_PUBLIC', 2);
19 * Public field, content shown on profile page and on member list pages.
21 define('PROFILE_PUBLIC_LISTINGS', 3);
24 * Hidden profile field, only accessible by administrators, modules and themes.
26 define('PROFILE_HIDDEN', 4);
29 * Implements hook_help().
31 function profile_help($path, $arg) {
33 case 'admin/help#profile':
35 $output .= '<h3>' . t('About') . '</h3>';
36 $output .= '<p>' . t('The Profile module allows site administrators to define custom fields (such as country, full name, or age) for user profiles, which are then displayed in the <a href="@user">My Account</a> section. This permits users of a site to share more information about themselves, and can help community-based sites organize users around specific information. For more information, see the online handbook entry for <a href="@profile">Profile module</a>.', array('@user' => url('user'), '@profile' => 'http://drupal.org/documentation/modules/profile/')) . '</p>';
37 $output .= '<h3>' . t('Uses') . '</h3>';
39 $output .= '<dt>' . t('Adding fields to the default profile') . '</dt>';
40 $output .= '<dd>' . t('To provide the ability for users to enter more information about themselves, the Profile module allows additional fields to be added to the default user profile. Examples of common additions would be <em>Location</em>, <em>Company</em>, <em>Age</em>, or <em>About me</em>.') . '</dd>';
41 $output .= '<dt>' . t('User information pages') . '</dt>';
42 $output .= '<dd>' . t('The Profile module enables links to see further information about site users. You can view both a main <a href="@profile">User list page</a>, and more specified pages by clicking on linked fields in any profile. For example, the <a href="@profile-country">People who live in Canada</a> listing on Drupal.org displays all users who have entered <em>Canada</em> in the <em>Country</em> field on their user profile.', array('@profile' => url('profile'), '@profile-country' => 'http://drupal.org/profile/country/Canada')) . '</dd>';
43 $output .= '<dt>' . t('Author information block') . '</dt>';
44 $output .= '<dd>' . t('The <em>Author information block</em> is a default block created by the Profile module that can be enabled on the <a href="@blocks">Blocks administration page</a>. It shows visitors of your website information about the author of the page they are reading.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
47 case 'admin/config/people/profile':
48 return '<p>' . t("This page displays a list of the existing custom profile fields to be displayed on a user's <em>My account</em> page. To provide structure, similar or related fields may be placed inside a category. To add a new category (or edit an existing one), edit a profile field and provide a new category name.") . '</p>';
53 * Implements hook_theme().
55 function profile_theme() {
57 'profile_block' => array(
58 'variables' => array('account' => NULL, 'fields' => array()),
59 'template' => 'profile-block',
61 'profile_listing' => array(
62 'variables' => array('account' => NULL, 'fields' => array()),
63 'template' => 'profile-listing',
65 'profile_wrapper' => array(
66 'variables' => array('content' => NULL),
67 'template' => 'profile-wrapper',
69 'profile_admin_overview' => array(
70 'render element' => 'form',
71 'file' => 'profile.admin.inc',
77 * Implements hook_menu().
79 function profile_menu() {
80 $items['profile'] = array(
81 'title' => 'User list',
82 'page callback' => 'profile_browse',
83 'access arguments' => array('access user profiles'),
84 'file' => 'profile.pages.inc',
85 'type' => MENU_SUGGESTED_ITEM,
87 $items['admin/config/people/profile'] = array(
88 'title' => 'Profiles',
89 'description' => 'Create customizable fields for your users.',
90 'page callback' => 'drupal_get_form',
91 'page arguments' => array('profile_admin_overview'),
92 'access arguments' => array('administer users'),
93 'file' => 'profile.admin.inc',
95 $items['admin/config/people/profile/add'] = array(
96 'title' => 'Add field',
97 'page callback' => 'drupal_get_form',
98 'page arguments' => array('profile_field_form'),
99 'access arguments' => array('administer users'),
100 'type' => MENU_VISIBLE_IN_BREADCRUMB,
101 'file' => 'profile.admin.inc',
103 $items['admin/config/people/profile/autocomplete'] = array(
104 'title' => 'Profile category autocomplete',
105 'page callback' => 'profile_admin_settings_autocomplete',
106 'access arguments' => array('administer users'),
107 'file' => 'profile.admin.inc',
108 'type' => MENU_CALLBACK,
110 $items['admin/config/people/profile/edit'] = array(
111 'title' => 'Edit field',
112 'page callback' => 'drupal_get_form',
113 'page arguments' => array('profile_field_form'),
114 'access arguments' => array('administer users'),
115 'type' => MENU_VISIBLE_IN_BREADCRUMB,
116 'file' => 'profile.admin.inc',
118 $items['admin/config/people/profile/delete'] = array(
119 'title' => 'Delete field',
120 'page callback' => 'drupal_get_form',
121 'page arguments' => array('profile_field_delete'),
122 'access arguments' => array('administer users'),
123 'type' => MENU_VISIBLE_IN_BREADCRUMB,
124 'file' => 'profile.admin.inc',
126 $items['profile/autocomplete'] = array(
127 'title' => 'Profile autocomplete',
128 'page callback' => 'profile_autocomplete',
129 'access arguments' => array('access user profiles'),
130 'file' => 'profile.pages.inc',
131 'type' => MENU_CALLBACK,
137 * Implements hook_block_info().
139 function profile_block_info() {
140 $blocks['author-information']['info'] = t('Author information');
141 $blocks['author-information']['cache'] = DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE;
146 * Implements hook_block_configure().
148 function profile_block_configure($delta = '') {
149 // Compile a list of fields to show
151 $result = db_query('SELECT name, title, weight, visibility FROM {profile_field} WHERE visibility IN (:visibility) ORDER BY weight', array(':visibility' => array(PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS)));
152 foreach ($result as $record) {
153 $fields[$record->name] = check_plain($record->title);
155 $fields['user_profile'] = t('Link to full user profile');
156 $form['profile_block_author_fields'] = array(
157 '#type' => 'checkboxes',
158 '#title' => t('Profile fields to display'),
159 '#default_value' => variable_get('profile_block_author_fields', array()),
160 '#options' => $fields,
161 '#description' => t('Select which profile fields you wish to display in the block. Only fields designated as public in the <a href="@profile-admin">profile field configuration</a> are available.', array('@profile-admin' => url('admin/config/people/profile'))),
167 * Implements hook_block_save().
169 function profile_block_save($delta = '', $edit = array()) {
170 variable_set('profile_block_author_fields', $edit['profile_block_author_fields']);
174 * Implements hook_block_view().
176 function profile_block_view($delta = '') {
177 if (user_access('access user profiles')) {
179 if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == NULL) {
180 $node = node_load(arg(1));
181 $account = user_load($node->uid);
183 if ($use_fields = variable_get('profile_block_author_fields', array())) {
184 // Compile a list of fields to show.
186 $result = db_query('SELECT * FROM {profile_field} WHERE visibility IN (:visibility) ORDER BY weight', array(':visibility' => array(PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS)));
187 foreach ($result as $record) {
188 // Ensure that field is displayed only if it is among the defined block fields and, if it is private, the user has appropriate permissions.
189 if (isset($use_fields[$record->name]) && $use_fields[$record->name]) {
195 if (!empty($fields)) {
196 $profile = _profile_update_user_fields($fields, $account);
197 $output .= theme('profile_block', array('account' => $account, 'fields' => $profile));
200 if (isset($use_fields['user_profile']) && $use_fields['user_profile']) {
201 $output .= '<div>' . l(t('View full user profile'), 'user/' . $account->uid) . '</div>';
206 $block['subject'] = t('About %name', array('%name' => format_username($account)));
207 $block['content'] = $output;
214 * Implements hook_user_presave().
216 function profile_user_presave(&$edit, $account, $category) {
217 if (!empty($account->uid)) {
218 profile_save_profile($edit, $account, $category);
223 * Implements hook_user_insert().
225 function profile_user_insert(&$edit, $account, $category) {
226 profile_save_profile($edit, $account, $category, TRUE);
230 * Implements hook_user_cancel().
232 function profile_user_cancel($edit, $account, $method) {
234 case 'user_cancel_reassign':
235 db_delete('profile_value')
236 ->condition('uid', $account->uid)
242 * Implements hook_user_delete().
244 function profile_user_delete($account) {
245 db_delete('profile_value')
246 ->condition('uid', $account->uid)
251 * Implements hook_user_load().
253 function profile_user_load($users) {
254 $result = db_query('SELECT f.name, f.type, v.uid, v.value FROM {profile_field} f INNER JOIN {profile_value} v ON f.fid = v.fid WHERE uid IN (:uids)', array(':uids' => array_keys($users)));
255 foreach ($result as $record) {
256 if (empty($users[$record->uid]->{$record->name})) {
257 $users[$record->uid]->{$record->name} = _profile_field_serialize($record->type) ? unserialize($record->value) : $record->value;
262 function profile_save_profile(&$edit, $account, $category, $register = FALSE) {
263 $result = _profile_get_fields($category, $register);
264 foreach ($result as $field) {
265 if (_profile_field_serialize($field->type)) {
266 $edit[$field->name] = serialize($edit[$field->name]);
268 db_merge('profile_value')
270 'fid' => $field->fid,
271 'uid' => $account->uid,
273 ->fields(array('value' => $edit[$field->name]))
278 function profile_view_field($account, $field) {
279 // Only allow browsing of private fields for admins, if browsing is enabled,
280 // and if a user has permission to view profiles. Note that this check is
281 // necessary because a user may always see their own profile.
282 $browse = user_access('access user profiles')
283 && (user_access('administer users') || $field->visibility != PROFILE_PRIVATE)
284 && !empty($field->page);
286 if (isset($account->{$field->name}) && $value = $account->{$field->name}) {
287 switch ($field->type) {
289 return check_markup($value, filter_default_format($account), '', TRUE);
292 return $browse ? l($value, 'profile/' . $field->name . '/' . $value) : check_plain($value);
294 return $browse ? l($field->title, 'profile/' . $field->name) : check_plain($field->title);
296 return '<a href="' . check_url($value) . '">' . check_plain($value) . '</a>';
298 $format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
299 // Note: Avoid PHP's date() because it does not handle dates before
300 // 1970 on Windows. This would make the date field useless for e.g.
303 'd' => sprintf('%02d', $value['day']),
304 'j' => $value['day'],
305 'm' => sprintf('%02d', $value['month']),
306 'M' => map_month($value['month']),
307 'Y' => $value['year'],
311 return strtr($format, $replace);
313 $values = preg_split("/[,\n\r]/", $value);
315 foreach ($values as $value) {
316 if ($value = trim($value)) {
317 $fields[] = $browse ? l($value, 'profile/' . $field->name . '/' . $value) : check_plain($value);
320 return implode(', ', $fields);
326 * Implements hook_user_view().
328 function profile_user_view($account) {
329 // Show private fields to administrators and people viewing their own account.
330 if (user_access('administer users') || $GLOBALS['user']->uid == $account->uid) {
331 $result = db_query('SELECT * FROM {profile_field} WHERE visibility <> :hidden ORDER BY category, weight', array(':hidden' => PROFILE_HIDDEN));
334 $result = db_query('SELECT * FROM {profile_field} WHERE visibility <> :private AND visibility <> :hidden ORDER BY category, weight', array(':private' => PROFILE_PRIVATE, ':hidden' => PROFILE_HIDDEN));
338 foreach ($result as $field) {
339 if ($value = profile_view_field($account, $field)) {
340 $title = ($field->type != 'checkbox') ? check_plain($field->title) : NULL;
342 // Create a single fieldset for each category.
343 if (!isset($account->content[$field->category])) {
344 $account->content[$field->category] = array(
345 '#type' => 'user_profile_category',
346 '#title' => $field->category,
350 $account->content[$field->category][$field->name] = array(
351 '#type' => 'user_profile_item',
354 '#weight' => $field->weight,
355 '#attributes' => array('class' => array('profile-' . $field->name)),
361 function _profile_form_explanation($field) {
362 $output = filter_xss_admin($field->explanation);
364 if ($field->type == 'list') {
365 $output .= ' ' . t('Put each item on a separate line or separate them by commas. No HTML allowed.');
368 if ($field->visibility == PROFILE_PRIVATE) {
369 $output .= ' ' . t('The content of this field is kept private and will not be shown publicly.');
376 * Implements hook_form_alter().
378 function profile_form_alter(&$form, &$form_state, $form_id) {
379 if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
382 $form['#validate'][] = 'profile_user_form_validate';
383 $account = $form['#user'];
384 $result = _profile_get_fields($form['#user_category'], $form['#user_category'] == 'register');
386 foreach ($result as $field) {
387 $category = $field->category;
388 if (!isset($form[$category])) {
389 $form[$category] = array('#type' => 'fieldset', '#title' => check_plain($category), '#weight' => $weight++);
391 switch ($field->type) {
394 $form[$category][$field->name] = array(
395 '#type' => 'textfield',
396 '#title' => check_plain($field->title),
397 '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
399 '#description' => _profile_form_explanation($field),
400 '#required' => $field->required,
402 if ($field->autocomplete) {
403 $form[$category][$field->name]['#autocomplete_path'] = "profile/autocomplete/" . $field->fid;
408 $form[$category][$field->name] = array(
409 '#type' => 'textarea',
410 '#title' => check_plain($field->title),
411 '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
412 '#description' => _profile_form_explanation($field),
413 '#required' => $field->required,
418 $form[$category][$field->name] = array(
419 '#type' => 'textarea',
420 '#title' => check_plain($field->title),
421 '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
422 '#description' => _profile_form_explanation($field),
423 '#required' => $field->required,
428 $form[$category][$field->name] = array(
429 '#type' => 'checkbox',
430 '#title' => check_plain($field->title),
431 '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
432 '#description' => _profile_form_explanation($field),
433 '#required' => $field->required,
439 $lines = preg_split("/[\n\r]/", $field->options);
440 foreach ($lines as $line) {
441 if ($line = trim($line)) {
442 $options[$line] = $line;
445 $form[$category][$field->name] = array(
447 '#title' => check_plain($field->title),
448 '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
449 '#options' => $options,
450 '#description' => _profile_form_explanation($field),
451 '#required' => $field->required,
457 $form[$category][$field->name] = array(
459 '#title' => check_plain($field->title),
460 '#default_value' => isset($account->{$field->name}) ? $account->{$field->name} : '',
461 '#description' => _profile_form_explanation($field),
462 '#required' => $field->required,
470 * Helper function: update an array of user fields by calling profile_view_field
472 function _profile_update_user_fields($fields, $account) {
473 foreach ($fields as $key => $field) {
474 $fields[$key]->value = profile_view_field($account, $field);
480 * Form validation handler for the user register/profile form.
482 * @see profile_form_alter()
484 function profile_user_form_validate($form, &$form_state) {
485 $result = _profile_get_fields($form['#user_category'], $form['#user_category'] == 'register');
486 foreach ($result as $field) {
487 if (!empty($form_state['values'][$field->name])) {
488 if ($field->type == 'url' && !valid_url($form_state['values'][$field->name], TRUE)) {
489 form_set_error($field->name, t('The value provided for %field is not a valid URL.', array('%field' => $field->title)));
492 elseif ($field->required && !user_access('administer users')) {
493 form_set_error($field->name, t('The field %field is required.', array('%field' => $field->title)));
499 * Implements hook_user_categories().
501 function profile_user_categories() {
502 $result = db_query("SELECT DISTINCT(category) FROM {profile_field}");
504 foreach ($result as $category) {
506 'name' => $category->category,
507 'title' => $category->category,
509 'access callback' => 'profile_category_access',
510 'access arguments' => array(1, $category->category)
517 * Menu item access callback - check if a user has access to a profile category.
519 function profile_category_access($account, $category) {
520 if (user_access('administer users') && $account->uid > 0) {
524 $category_visible = (bool) db_query_range('SELECT 1 FROM {profile_field} WHERE category = :category AND visibility <> :visibility', 0, 1, array(
525 ':category' => $category,
526 ':visibility' => PROFILE_HIDDEN
528 return user_edit_access($account) && $category_visible;
533 * Process variables for profile-block.tpl.php.
535 * The $variables array contains the following arguments:
539 * @see profile-block.tpl.php
541 function template_preprocess_profile_block(&$variables) {
543 $variables['user_picture'] = theme('user_picture', array('account' => $variables['account']));
544 $variables['profile'] = array();
545 // Supply filtered version of $fields that have values.
546 foreach ($variables['fields'] as $field) {
548 $variables['profile'][$field->name] = new stdClass();
549 $variables['profile'][$field->name]->title = check_plain($field->title);
550 $variables['profile'][$field->name]->value = $field->value;
551 $variables['profile'][$field->name]->type = $field->type;
558 * Process variables for profile-listing.tpl.php.
560 * The $variables array contains the following arguments:
564 * @see profile-listing.tpl.php
566 function template_preprocess_profile_listing(&$variables) {
568 $variables['user_picture'] = theme('user_picture', array('account' => $variables['account']));
569 $variables['name'] = theme('username', array('account' => $variables['account']));
570 $variables['profile'] = array();
571 // Supply filtered version of $fields that have values.
572 foreach ($variables['fields'] as $field) {
574 $variables['profile'][$field->name] = new stdClass();
575 $variables['profile'][$field->name]->title = $field->title;
576 $variables['profile'][$field->name]->value = $field->value;
577 $variables['profile'][$field->name]->type = $field->type;
584 * Process variables for profile-wrapper.tpl.php.
586 * The $variables array contains the following arguments:
589 * @see profile-wrapper.tpl.php
591 function template_preprocess_profile_wrapper(&$variables) {
592 $variables['current_field'] = '';
593 if ($field = arg(1)) {
594 $variables['current_field'] = $field;
595 $variables['theme_hook_suggestions'][] = 'profile_wrapper__' . $field;
599 function _profile_field_types($type = NULL) {
600 $types = array('textfield' => t('single-line textfield'),
601 'textarea' => t('multi-line textfield'),
602 'checkbox' => t('checkbox'),
603 'selection' => t('list selection'),
604 'list' => t('freeform list'),
606 'date' => t('date'));
607 return isset($type) ? $types[$type] : $types;
610 function _profile_field_serialize($type = NULL) {
611 return $type == 'date';
614 function _profile_get_fields($category, $register = FALSE) {
615 $query = db_select('profile_field');
617 $query->condition('register', 1);
620 $query->condition('category', db_like($category), 'LIKE');
622 if (!user_access('administer users')) {
623 $query->condition('visibility', PROFILE_HIDDEN, '<>');
626 ->fields('profile_field')
627 ->orderBy('category', 'ASC')
628 ->orderBy('weight', 'ASC')