Commit 1ba2aa18 authored by Dave Reid's avatar Dave Reid
Browse files

#684438 by Dave Reid: Ported realname to D7.

parent 2e3e970f
<?php
function realname_settings_form($form, &$form_state) {
$form['realname_pattern'] = array(
'#type' => 'textfield',
'#title' => t('Realname pattern'),
'#default_value' => variable_get('realname_pattern', '[user:name-raw]'),
'#element_validate' => array('token_element_validate'),
'#token_types' => array('user'),
'#min_tokens' => 1,
'#required' => TRUE,
);
// Add the token tree UI.
$form['token_help'] = array(
'#theme' => 'token_tree',
'#token_types' => array('user'),
'#global_types' => FALSE,
);
$form['#submit'][] = 'realname_settings_form_submit';
return system_settings_form($form);
}
function realname_settings_form_submit($form, $form_state) {
if ($form['realname_pattern']['#default_value'] != $form_state['values']['realname_pattern']) {
// Only clear the realname cache if the pattern was changed.
realname_delete_all();
}
}
<?php
function hook_realname_pattern_alter(&$pattern, $account) {
}
function hook_realname_clear($account = NULL) {
}
\ No newline at end of file
; $Id$
name = "RealName"
description = "Use user profile fields to build a 'realname' field in a user object."
dependencies = profile
name = Real name
description = Provides token-based name displays for users.
core = 7.x
dependencies[] = token
files[] = realname.module
files[] = realname.admin.inc
files[] = realname.pages.inc
files[] = realname.tokens.inc
files[] = realname.test
files[] = realname.install
configure = admin/config/people/realname
<?php
// $Id$
/**
* @file
* Handles installation and updates for the RealName module.
*
* @copyright Copyright (c) 2007-2008 Nancy Wichmann. All rights reserved.
* Implements hook_schema().
*/
function realname_schema() {
$schema['realname'] = array(
'description' => 'Computed Real Names to reduce overhead.',
'fields' => array(
'uid' => array(
'description' => 'User ID, links to User table.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'realname' => array(
'description' => 'The generated real name of the user.',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
),
'created' => array(
'description' => 'The UNIX timestamp of when the real name was created.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('uid'),
'indexes' => array(
'realname' => array('realname')
),
'foreign keys' => array(
'users' => array(
'table' => 'users',
'columns' => array('uid' => 'uid'),
),
),
);
//********************************************************************
//* Drupal Hooks
//********************************************************************
return $schema;
}
/**
* Implementation of hook_enable().
* Implements hook_uninstall().
*/
function realname_enable() {
drupal_set_message(t('The RealName module has been enabled. You may wish to proceed to <a href="!url">the settings page</a>.', array('!url' => url('admin/user/realname'))));
function realname_uninstall() {
variable_del('realname_pattern');
}
/**
* Implementation of hook_uninstall().
* Update the {realname} table for Drupal 7.
*/
function realname_uninstall() {
variable_del('realname_pattern');
variable_del('realname_fields');
variable_del('realname_theme');
variable_del('realname_nodeapi');
function realname_update_7000() {
// Add the {realname}.created field if it doesn't already exist.
if (!db_field_exists('realname', 'created')) {
$fields['created'] = array(
'description' => 'The UNIX timestamp of when the real name was created.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
);
db_add_field('realname', 'created', $fields['created']);
}
$fields['realname'] = array(
'description' => 'The generated real name of the user.',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
);
db_change_field('realname', 'realname', 'realname', $fields['realname']);
}
<?php
// $Id$
// @todo Alter the username autocomplete
// @todo Allow users to login with their real name
// @todo Disable the username field
// @todo Allow users to override their real name
/**
* @file
* The RealName module allows the admin to choose fields from the user profile
* that will be used to add a "realname" element (method) to a user object.
* Hook_user is used to automatically add this to any user object that is loaded.
*
* @copyright Copyright (c) 2007-2008 Nancy Wichmann. All rights reserved.
* Implements hook_permission().
*/
//********************************************************************
//* Drupal Hooks
//********************************************************************/
function realname_permission() {
$permissions['administer realname'] = array(
'title' => t('Administer Real Name configuration.'),
);
return $permissions;
}
/**
* Implementation of hook_help().
* Implements hook_menu().
*/
function realname_help($path, $args = null) {
switch ($path) {
case 'admin/user/realname':
return t('This page displays the status of and settings for the RealName module.');
function realname_menu() {
$items['admin/config/people/realname'] = array(
'title' => 'Real name',
'page callback' => 'drupal_get_form',
'page arguments' => array('realname_settings_form'),
'access arguments' => array('administer realname'),
'file' => 'realname.admin.inc',
);
case 'admin/help#realname':
return t('This simple module adds a "realname" element (method) to a user object when that object is loaded.');
}
return $items;
}
/**
* Implementation of hook_menu().
* Implements hook_username_alter().
*/
function realname_menu($may_cache) {
global $user;
$items = array();
function realname_username_alter(&$name, stdClass $account) {
static $in_username_alter = FALSE;
if ($may_cache) {
$items[] = array(
'path' => 'admin/user/realname',
'title' => t('RealName'),
'description' => t("Configure which fields are used to create a user's RealName."),
'access' => user_access('administer users'),
'callback' => 'drupal_get_form',
'callback arguments' => array('realname_admin_settings'),
);
if (isset($account->realname)) {
$name = $account->realname;
}
else {
// If desired, load the theme override file.
if (variable_get('realname_theme', false)) {
include_once(drupal_get_path('module', 'realname') .'/realname_theme.inc');
elseif (!$in_username_alter) {
// Because realname tokens may execute format_username() we need to prevent
// recursion from happening.
$in_username_alter = TRUE;
if ($realname = realname_load($account)) {
$name = $realname;
}
$in_username_alter = FALSE;
}
return $items;
}
/**
* Implementation of hook_user().
* Implements hook_user_load().
*/
function realname_user($op, &$edit, &$account, $category = null) {
if ($op == 'load') {
$account->realname = realname_make_name($account);
function realname_user_load(array $accounts) {
$realnames = realname_load_multiple($accounts);
foreach ($realnames as $uid => $realname) {
$accounts[$uid]->realname = $realname;
}
}
/**
* Implementation of hook_nodeapi().
* Implements hook_user_update().
*/
function realname_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if (!variable_get('realname_nodeapi', false)) {
return;
}
switch ($op) {
case 'load':
// Node is being loaded.
// Save the username that is already there.
$node->realname_save = $node->name;
$account = user_load(array('uid' => $node->uid));
$node->realname = $node->name = realname_make_name($account);
break;
case 'prepare':
// Node is about to be edited.
// Reset the username or save will fail.
if (isset($node->realname_save)) {
$node->name = $node->realname_save;
}
break;
}
function realname_user_update(array &$edit, stdClass $account, $category) {
// Since user data may have changed, delete the existing realname.
realname_delete($account->uid);
}
//********************************************************************
//* Module Functions
//********************************************************************
/**
* Implements hook_user_delete().
*/
function realname_user_delete(stdClass $account) {
realname_delete($account->uid);
}
/**
* Using selected fields, build the "real name" field in the object.
*
* @param
* $account - the user object to update.
* Loads a real name.
*
* @param $uid
* A user account object.
* @return
* The constructed "real name" string.
* The user's generated real name.
*/
function realname_make_name(&$account) {
$fields = variable_get('realname_fields', array());
// $sep = variable_get('realname_sep', ' ');
$pattern = variable_get('realname_pattern', ' ');
// Has the profile been loaded?
if (!isset($account->{$fields[0]['name']})) {
profile_load_profile($account);
}
function realname_load(stdClass $account) {
$realnames = realname_load_multiple(array($account->uid => $account));
return reset($realnames);
}
$stuff = array();
$i = 0;
foreach ($fields as $name => $weight) {
++$i;
if (isset($account->$name)) {
$stuff['%'. $i] = check_plain($account->$name);
}
else {
// If there is no value, remove the patterm piece, except the first.
$pattern = $i > 1 ? str_replace('%'. $i, null, $pattern) : $pattern;
/**
* Loads multiple real names.
*
* @param $accounts
* An array of user account objects keyed by user ID.
* @return
* An array of real names keyed by user ID.
*/
function realname_load_multiple(array $accounts) {
$realnames = &drupal_static(__FUNCTION__, array());
if ($new_accounts = array_diff_key($accounts, $realnames)) {
// Attempt to fetch realnames from the database first.
$realnames += db_query("SELECT uid, realname FROM {realname} WHERE uid IN (:uids)", array(':uids' => array_keys($new_accounts)))->fetchAllKeyed();
// For each account that was not present in the database, generate its
// real name.
foreach ($new_accounts as $uid => $account) {
if (!isset($realnames[$uid]) || !drupal_strlen($realnames[$uid])) {
// Get the default pattern and allow other modules to alter it.
$pattern = variable_get('realname_pattern', '[user:name-raw]');
drupal_alter('realname_pattern', $pattern, $account);
// Perform token replacement on the real name pattern.
$realname = token_replace($pattern, array('user' => $account), array('clear' => TRUE));
$realname = trim($realname);
module_invoke_all('realname_load', $realname, $account);
// Save to the database and the static cache.
db_merge('realname')
->key(array('uid' => $uid))
->fields(array(
'realname' => $realname,
'created' => REQUEST_TIME,
))
->execute();
$realnames[$uid] = $realname;
}
}
}
// If no fields set, use username.
if (count($stuff) == 0) {
$stuff['%1'] = $account->name;
}
// TODO: Make a pattern, rather than hard separator.
$string = trim(strtr($pattern, $stuff));
return $string;
return array_intersect_key($realnames, $accounts);
}
/**
* Provides array sorting function for uasort.
* @link http://us2.php.net/manual/en/function.uasort.php PHP Manual @end-link
*
* @param
* $a - the first array to be compared.
* Delete a real name.
*
* @param
* $b - the second array to be compared.
*
* @return
* integer indicating ordering.
* @param $uid
* A user ID.
*/
function _realname_sort($a, $b) {
// Sort first by weight.
$ret = $a['weight'] - $b['weight'];
if ($ret == 0) {
// The two are equal, so use the title.
$ret = strcmp($a['title'], $b['title']);
}
return $ret;
function realname_delete($uid) {
return realname_delete_multiple(array($uid));
}
/**
* Displays the admin settings form.
* Delete multiple real names.
*
* @param $uids
* An array of user IDs.
*/
function realname_admin_settings() {
$form = $fields = array();
$current = variable_get('realname_fields', array());
$result = db_query("SELECT * FROM {profile_fields} WHERE type='textfield'");
while ($field = db_fetch_array($result)) {
$name = $field['name'];
$selected = array_key_exists($name, $current);
$fields[$name] = array(
'title' => $field['title'],
'weight' => $selected ? $current[$name] : 0,
'selected' => $selected,
);
}
uasort($fields, '_realname_sort');
$form['start_table'] = array(
'#type' => 'markup',
'#value' => '<table><tr><th>Select</th><th>Field name</th><th>Weight</th></tr>',
);
$i = 0;
foreach ($fields as $f_name => $values) {
$form['field_select_'. $i] = array(
'#type' => 'checkbox',
'#default_value' => $values['selected'],
'#prefix' => '<tr><td align="center">',
'#suffix' => '</td>',
);
$form['field_name_'. $i] = array(
'#type' => 'hidden',
'#value' => $f_name,
);
$form['field_title_'. $i] = array(
'#type' => 'item',
'#value' => $values['title'],
'#prefix' => '<td>',
'#suffix' => '</td>',
);
$form['field_weight_'. $i] = array(
'#type' => 'weight',
'#delta' => 10,
'#default_value' => $values['weight'],
'#prefix' => '<td>',
'#suffix' => '</td></tr>',
);
++$i;
}
$form['end_table'] = array(
'#type' => 'markup',
'#value' => '</table>',
);
$form['realname_pattern'] = array(
'#type' => 'textfield',
'#field_prefix' => '<strong>'. t('Name Pattern') .'</strong> ',
'#description' => t('The determines how the fields will be used to create the "Real name." Use "%1" to refer to the first field, "%2" to refer to the second, etc..'),
'#size' => 30,
'#default_value' => variable_get('realname_pattern', '%1'),
);
$form['realname_theme'] = array(
'#type' => 'checkbox',
'#title' => '<strong>'. t('Override username theme') .'</strong>',
'#description' => t('If this option is selected, the standard username theme function will be overriden to use the "Real name."'),
'#default_value' => variable_get('realname_theme', false),
);
$form['realname_nodeapi'] = array(
'#type' => 'checkbox',
'#title' => '<strong>'. t('Show realname in nodes') .'</strong>',
'#description' => t('If this option is selected, the "Real name" will be used on node displays.'),
'#default_value' => variable_get('realname_nodeapi', false),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
function realname_delete_multiple(array $uids) {
db_delete('realname')->condition('uid', $uids, 'IN')->execute();
drupal_static_reset('realname_load_multiple');
}
/**
* Form submit handler.
* Delete all real names.
*/
function realname_admin_settings_submit($form_id, $form_values) {
$i = 0;
$fields = array();
// Run the form values to get all the fields they want.
while (isset($form_values['field_select_'. $i])) {
if ($form_values['field_select_'. $i]) {
$fields[] = array('title' => $form_values['field_name_'. $i], 'weight' => $form_values['field_weight_'. $i]);
}
++$i;
}
// A little hoop jumping to sort right.
uasort($fields, '_realname_sort');
$realname_fields = array();
foreach ($fields as $key => $values) {
$realname_fields[$values['title']] = $values['weight'];
}
// Okay, save the stuff.
variable_set('realname_fields', $realname_fields);
// variable_set('realname_sep', $form_values['realname_sep']);
variable_set('realname_pattern', $form_values['realname_pattern']);
variable_set('realname_theme', $form_values['realname_theme']);
variable_set('realname_nodeapi', $form_values['realname_nodeapi']);
drupal_set_message(t('Configuration has been updated.'), 'status');
function realname_delete_all() {
db_delete('realname')->execute();
drupal_static_reset('realname_load_multiple');
}
<?php
/**
* @file
* Builds placeholder replacement tokens for realname.
*/
/**
* Implements hook_token_info().
*/
function realname_token_info() {
$info['tokens']['user']['name-raw'] = array(
'name' => t('Raw name'),
'description' => t('The raw login name of the user account, unprocessed by the RealName module.'),
);
return $info;
}
/**
* Implements hook_token_info_alter().
*/
function realname_token_info_alter(&$info) {
// Since the [user:name] token uses format_username(), its meta-data should
// be altered to reflect that it will be the real name.
$info['tokens']['user']['name']['name'] = t('Real name');
$info['tokens']['user']['name']['description'] = t('The real name of the user account.');
}
/**
* Implements hook_tokens().
*/
function realname_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
if ($type == 'user' && !empty($data['user'])) {
$account = $data['user'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'name-raw':
$replacements[$original] = $sanitize ? check_plain($account->name) : $account->name;
break;
}
}
$user_fields = field_info_instances('user', 'user');
foreach ($user_fields as $field_name => $field) {
$token_name = strtr($field_name, '_', '-');
if ($field['widget']['module'] == 'text' && isset($tokens[$token_name]) && !empty($account->{$field_name}[LANGUAGE_NONE][0])) {
$original = $tokens[$token_name];
$item = $account->{$field_name}[LANGUAGE_NONE][0];
$instance = field_info_instance('user', $field_name, 'user');
$replacements[$original] = $sanitize ? _text_sanitize($instance, LANGUAGE_NONE, $item, 'value') : $item['value'];
}
}
}
return $replacements;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment