Real Name
Hey all,
Well, after downloading Drupal a couple of weeks ago, I'm CLOSE to putting up my site (thanks to these GREAT forums). I have a question that I can't figure out.
The site is for my old high school class and I'm wondering about username vs real name. It's really much easier to sign in as, say, kathyj, but it would be nice if the name appearing on her blogs would indicate Kathy Smith Johnson (first-maiden-last) so everyone knows who she is. I've managed to add an extra field (Real Name) through administer -> settings -> profiles, but how can I get this to show on the "posted by"?
The obvious way is to make users sign in WITH the real name, but it sure would make it easier if there was a module or hack to accomplish this. I'm using 4.7.6.
Thanks.

Authorship module?
Would the Authorship Module be suitable for what you want to achieve?
From the module's readme.txt:
user use authorshipThis allows a site administrator let regular users manage the
display of their own name (trusted users). Create a role and
set this perm to that role. Then, users in that role will be
able to manage the display of their name in much the same way
site administrators can but only for their own posts.
I'm sure there's a way to do it using your custom profile field, but this is could be an easily implemented alternative.
You can over ride the theme function username
Printing of the user name is controlled by the theme function theme_username which can be overridden. Add the function below to your themes template.php file. If you do not have a template.php file you will need to create one, make sure it starts with <?php. Replace 'YOUR_PROFILE_FIELD' with the actual field name you used. Note that the users real name will pretty much print anywhere there user name used to print.
function phptemplate_username($object, $link = TRUE) {
if ( !$object->YOUR_PROFILE_FIELD ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->YOUR_PROFILE_FIELD ) {
$name = $object->YOUR_PROFILE_FIELD;
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
// Profile field not set, default to standard behaviour
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $object->name;
}
if ( $link && user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = check_plain($name);
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = l($object->name, $object->homepage);
}
else {
$output = check_plain($object->name);
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
Real Name
Thanks for the suggestions.
I decided to go the "theme template" route. It works like a charm!!
Have been looking for the
Have been looking for the same function. Thanks for sharing!!!
Cheers,
Rob
This method does not work
This method does not work for me -- is this the only file I need to edit? Running 5.1.
Can you expand on does not work?
While your only need to edit template.php to add the function you do need to edit the function so it uses your profile field and you need to have defined the profile field.
Thanks for your reply
Thanks for your reply nevets! Here is the content of my template.php file. I've made the neccessary changes -- the field is called profile_fullname.
In each place where it should display the full name, it just shows the username. I've tried the userlist, as well as forums, and they behave the same.
Can you see anything I'm not doing correctly?
<?php
// $Id: template.php,v 1.1.2.2 2007/04/10 00:28:25 jwolf Exp $
// regions for barron
function barron_regions() {
return array(
'sidebar_left' => t('sidebar'),
'content_top' => t('content top'),
'content_bottom' => t('content bottom'),
'footer' => t('footer')
);
}
// sets delimiter and styles for $links
function phptemplate_links($links, $attributes = array('class' => 'links')) {
$output = '';
if (count($links) > 0) {
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
$class = '';
// Automatically add a class to each link and also to each LI
if (isset($link['attributes']) && isset($link['attributes']['class'])) {
$link['attributes']['class'] .= ' ' . $key;
$class = $key;
}
else {
$link['attributes']['class'] = $key;
$class = $key;
}
// Add first and last classes to the list of links to help out themers.
$extra_class = '';
if ($i == 1) {
$extra_class .= 'first ';
} else {
$output .= ' • ';
}
if ($i == $num_links) {
$extra_class .= 'last ';
}
$output .= '<span class="'. $extra_class . $class .'">';
// Is the title HTML?
$html = isset($link['html']) && $link['html'];
// Initialize fragment and query variables.
$link['query'] = isset($link['query']) ? $link['query'] : NULL;
$link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;
if (isset($link['href'])) {
$output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
}
else if ($link['title']) {
//Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (!$html) {
$link['title'] = check_plain($link['title']);
}
$output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
}
$i++;
$output .= "</span>\n";
}
}
return $output;
}
function phptemplate_username($object, $link = TRUE) {
if ( !$object->profile_fullname ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->profile_fullname ) {
$name = $object->profile_fullname;
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
// Profile field not set, default to standard behaviour
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $object->name;
}
if ( $link && user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = check_plain($name);
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = l($object->name, $object->homepage);
}
else {
$output = check_plain($object->name);
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
Nothing obvious
So I would change (at the beginning of the function)
if ( !$object->profile_fullname ) {if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
to add some code to understand what is happening
drupal_set_message("phptemplate_username: entered");lif ( !$object->profile_fullname ) {
drupal_set_message("phptemplate_username: need to load user profile for $object->uid");l
if ( $object->uid && function_exists('profile_load_profile') ) {
drupal_set_message("phptemplate_username: load profile");
profile_load_profile($object);
}
}
So if you add the three call to drupal_set_message(), which if any show up when view a node for instance? If the all show up after the call to profile_load_profile you could add
drupal_set_message("Profile fields<pre>" . print_r($object, TRUE) . '</pre>');which dump all the fields in object. Do you see the one you expect?
Sorry, I'm new at drupal --
Sorry, I'm new at drupal -- which section of code do I post at the beginning of template.php? If it's easier, just append your code to my original and then I'll have exactly what it should look like.
The new code replaces the first five lines of
The new code replaces the first five lines of the function phptemplate_username().
Error
Now I'm getting this error:
Parse error: syntax error, unexpected T_IF in /home/.kumquat/tzpikes/tzpikes.com/pikeaccess/themes/barron/template.php on line 77
Probably the extra characters
Lines 1 and 3 have an extra character after the semicolan (;), looks a lowercase L (l).
drupal_set_message("phptemplate_username: entered");lif ( !$object->profile_fullname ) {
drupal_set_message("phptemplate_username: need to load user profile for $object->uid");l
doh!
I wish there was a "delete" option for these posts. After racking my brains, messing around with the code you gave me - I was stumped. I finally checked my user profile (as I'm the only user currently) and looked in the personal info. My "full name" field was blank. So obviously it was defaulting to the username. Once I put my name in there (I swear it was in there already), everything works great.
Sorry -- one of those "doh" moments. Thanks for the help!!
Sorry...dumb
I was just browsing through the node and I tried to add the coding you suggested to use full names instead of usernames. I know you say you have to define the profile field first though. I don't exactly understand what you mean by that..Could you explain how I would do that? Thanks...sorry I'm new to computer programming in general
Navigation block?
Ok, got it working. Thanks! Last question: Is there a way to change the display of the username to the full name on the navigation block? The username still appears in both the navigation block and the profile itself (profile isn't a big deal though, since the real name displays there as well)
A More complete change
I have applied this and it works good for certain places on the site, but not in others. I am using the Blog module, and when viewing blog entry on the front page, the name appears at the top, but not at the bottom... for example, it says:
Submitted by [real name] on [date and time]
just underneath the title, but below the blog post it says:
[username]'s blog
as a link to the user's blogs.
Also, the hover over on the bottom one says Read [username]'s latest blog entries. but that is less important to switch. Any help on how to modify the template above to work for this as well?
Changes to blog.module
Hi Solhan
I don't really recommend this, but you could make changes to the blog module (Beware of problems when you upgrade later!) Can anyone suggest a better way?
This is what I did on 5.1
Open the blog.module in a text editor, and look for occurances of $node->name
replace with:
theme('username', $node, FALSE)I think there are 3 occurances of $node->name in 2 different functions. One (function blog_view) at line 219) deals with the breadcumbs, the other (function blog_link at line 233) with the link beneath the posts and the mousover message.
Finally, you can change the 'username's blog' title by altering the blog_page_user function (line 128) and replacing $account->name (line 134) with
theme('username', $account, FALSE)See how you go, but I'm sure there must be a better way to overide core module functions than this.
Speaking of Overriding Core Modules...
Check out this article on the proper way to do it...
http://www.bryght.com/blog/boris-mann/cleanly-overriding-core-modules-in...
I've implemented this
I've implemented this code... it works fine in many places, except for "Add username to my buddylist", and also inside private messaging.
But what I noticed is that my site slowed down quite a lot. Is it because of this code, or could it be some other module that I've implemented???
In short, my question to those who have implemented this code is: does this code slow down your site???
I have not seen a performance hit
I have not seen a performance hit but if one defines a "large" profile (i.e. lots of profile fields) it is possible that it might slow things down enough to notice. The code will increase page load times since it loads user profiles, the question is will it be enough to notice. You could use the devel module to see sql and page load times and compare performance with and without the theme function.
As for buddylist, private messaging, etc, this approach only works for code that uses the theme function, any code that uses $user->name will show the standard Drupal name,
worked for me
+1. I just added in nevets' patch and it seems to work perfectly. Thanks!
Drupal 6 compatible?
Will this code work ok for Drupal 6?
Thanks!
Mass contact real name
I'm using the mass contact module and wondered if I could fill populate the real name in the "name" textbox instead of the username. Here is the relevant code, I think. I've tinkered a bit, substituting profile_fullname in a few places, but it hasn't worked. How can I populate the textbox "name" with the user's full name? I'm using nevets code throughout the rest of the site. I've tried posting a support request on the module's page, but nothing so far - so I thought I'd try my luck here.
function mass_contact_mail_page() {
global $user;
$result = db_query('SELECT cid, category, selected FROM {mass_contact} ORDER BY weight, category');
while ($category = db_fetch_object($result)) {
$categories[$category->cid] = $category->category;
if ($category->selected) {
$default_category = $category->cid;
}
}
if (count($categories) > 0) {
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['#token'] = $user->name . $user->mail;
$form['contact_information'] = array('#value' => filter_xss_admin(variable_get('mass_contact_form_information', t('Send an email message using the contact form below.'))));
$form['name'] = array('#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 255,
'#default_value' => $user->uid ? $user->name : '',
'#required' => TRUE,
One approach is to modify the code
While you could make a module that implements the form_alter hook it would be simplier to modify this line
'#default_value' => $user->uid ? $user->name : '',to
'#default_value' => $user->uid ? theme('username', $user->name, FALSE) : '',Anonymous
Thanks for the reply, nevets. I tried your suggestion, but it filled in "Anonymous" instead of the name.
Sorry, I answer to quickly
Here is the correct new version of the line
'#default_value' => $user->uid ? theme('username', $user, FALSE) : '',The $user->name should be $user
Excellent! That worked,
Excellent! That worked, thanks nevets! Any ideas about the navigation block comment I posted above?
It comes from the user module
If you look at the block hook in user.module you will find the theme function is not used and so the code will not make a difference here. While one could modify the code to call the 'username' theme function, being part of core it is generally not recommended since it makes updating you site harder.
Thanks
Ok, thanks for clearing it up. You've been a huge help. Thanks again!!
Minor glitch: /admin/user/user no longer displays username
This fix is really cool.
There's one minor glitch though: if you look at the list of users (at /admin/user/user) the column that says "username" no longer shows the username, but rather the Full Name (if it exists). Also, the sorting of the username column is done based on the underlying username, not the Full Name that the user sees.
-mark
Another problem: Full name of newly added users is missing
If you install the above fix and then add a new user, their Full Name does not display because it does not get into the profile_values table.
A quick workaround is Edit their user entry, go to the the Personal Information panel for the user, and hit the Submit button.
A better fix that seems to work is to get the patch from http://drupal.org/node/119114#comment-219903
-mark
setting the user name to not set or anon
Hi,
Just wondering if it's possible to set the user name to anon or something similar if the profile field has not been filled in. I don't want any occurrences of the username on the site at all.
Any help would be great!
Cheers,
Sarah
Yes
Find the comment
// Profile field not set, default to standard behaviourFor only people logged in after the comment add
if ( $object->uid ) {$object->name = "Your Default Name";
}
If you want to include people not logged in it would be
$object->name = "Your Default Name";Real name
I think this module does what you want.
http://drupal.org/project/alt_login
i just did a dodgy hack
thanks for your help i just did a dodgy hack and it works ok
function phptemplate_username($object, $link = TRUE) {
if ( !$object->profile_displayName ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->profile_displayName ) {
$name = $object->profile_displayName;
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
// Profile field not set, default to standard behaviour
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
/*$name = drupal_substr($object->name, 0, 15) .'...';*/
$name = 'Unknown';
}
else {
/*$name = $object->name;*/
$name = 'Unknown';
}
if ( $link && user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
/*$output = check_plain($name);*/
$output = 'Unknown';
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = l('Unknown' . $object->homepage);
}
else {
/*$output = check_plain($object->name);*/
$output = 'Unknown';
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
Who is Online with full names
Is it perhaps possible that just in the 'Who is Online' to have the full names? I have 2 fields which are profile_name and profile_lname.
<?php
$number = db_result(db_query('SELECT COUNT(uid) AS number FROM {users} WHERE status=1'));
if (user_access('access content')) {
// Count users with activity in the past defined period.
$time_period = variable_get('user_block_seconds_online', 900);
// Perform database queries to gather online user lists.
$guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
$users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 1 ORDER BY access DESC', time() - $time_period);
$total_users = db_num_rows($users);
// Format the output with proper grammar.
echo "Out of $number registered users ";
if ($total_users == 1 && $guests->count == 1) {
$output = t('%members and %visitors online.', array('%members' => format_plural($total_users, 'there is currently 1 user', 'there are currently @count users'), '%visitors' => format_plural($guests->count, '1 guest', '@count guests')));
}
else {
$output = t('there are currently %members and %visitors online.', array('%members' => format_plural($total_users, '1 user', '@count users'), '%visitors' => format_plural($guests->count, '1 guest', '@count guests')));
}
// Display a list of currently online users.
$max_users = variable_get('user_block_max_list_count', 10);
if ($total_users && $max_users) {
$items = array();
while ($max_users-- && $account = db_fetch_object($users)) {
$items[] = $account;
}
$output .= theme('user_list', $items, t('Online users'));
}
}
return $output;
?>
That would be absolutely stunning for my site as most members know each other.
Look forward to any reply.
Lilian
This may work
The code you show uses theme_user_list to provide the look of the list. In core this seems to be only used by "Who's Online" and "Who's New". If changing both this list is ok, you could add the following to your themes template.php file
function phptemplate_user_list($users, $title = NULL) {if (!empty($users)) {
foreach ($users as $user) {
$items[] = theme('fullname', $user);
}
}
return theme('item_list', $items, $title);
}
Now looking at comment http://drupal.org/node/122303#comment-204277 take the code there and also add it to your template.php. Change the function name from phptemplate_username to phptemplate_fullname and I think you will have it. You will also need to adapt the code to use your profile fields to get the name information.
Real Name
Thank you Nevets for your response!
Nevets, previously the code below works fine and it shows the first name of the person in 'who is online' and 'who is new'. My profile fields for name and last name are profile_fname and profile_lname.
Maybe I'm missing something here, but sorry, do not have profile_fullname... Is there perhaps a way to include both the fields please?
<?php
function phptemplate_username($object, $link = TRUE) {
if ( !$object->profile_fname ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->profile_fname ) {
$name = $object->profile_fname;
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
// Profile field not set, default to standard behaviour
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $object->name;
}
if ( $link && user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = check_plain($name);
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = l($object->name, $object->homepage);
}
else {
$output = check_plain($object->name);
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
?>
Thank you so much for any help, most appreciated.
Lilian
Sorry, I'm sure a silly question .. but how do I get notified by email when there is a response on the forum?
I think I am confused
I originally thought you only want the names to show in the block, and that is what my first answer was about. If you have the above already working you should see the full names in the block.
(Me, I just check back every so often)
real name when using node profile and not profile.module
Hi there,
i would like to display the real name of users throughout my site also. But i am not using the profile module, but am creating profiles using nodes and content types.
i have fields set up for first name and last name as 'content_type_first_name' and 'content_type_last_name' which are related to my profile node, which is a child of the parent usernode.
Anyone know how it is possible to override the username with the first name and last name in this case?
sylvie
Something like this should load a node based profile
For loading a node based profile you need to know the have the content type of the node used for the node profile
<?phpif ( $object->uid ) {
$type = "my_profile";
$sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'");
$nid = db_result(db_query($sql, $object->uid, $type));
if ( $nid ) {
$profile = node_load($nid);
// You can use print_r($profile) to see the fields
}
}
?>
eek
hiya nevets,
thanks for the code. I would like to replace username throughout my site with the first name and last name. i'm sorry, but i'm one of those annoying novices(!) - would i put the above code in the template.php file? how would i print the specific fields of first name and last name?
thanks!
Goes in the template, replaces part of my original post
Here is the start of the original post
function phptemplate_username($object, $link = TRUE) {
if ( !$object->YOUR_PROFILE_FIELD ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->YOUR_PROFILE_FIELD ) {
Replace that with this fixed version
function phptemplate_username($object, $link = TRUE) {if ( $object->uid ) {
$type = "my_profile";
$sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'");
$nid = db_result(db_query($sql, $object->uid, $type));
if ( $nid ) {
$profile = node_load($nid);
}
}
// You will need to replace $object->YOUR_PROFILE_FIELD with
// $profile->your_field_name
// the details depend on how you have created to content type
// and which version of Drupal you are using
// The following line will show you the available fields
print '<pre>' . print_r($profile, TRUE) . '</pre>';
if ( $object->YOUR_PROFILE_FIELD ) {
blank screen
I am using latest drupal - 5.2
I copied code (the part you just posted and the rest from the top of this post) into template.php, and replaced YOUR_PROFILE_FIELD with field_first_name, and got a blank screen as a response. My first name content is located in my database as "content_field_first_name" (on drupal admin pages it is named field_first_name. This field has been created as part of the content type - "uprofile", which is attached to my usernode with nodeprofile.
I have followed the tutorial at http://shellmultimedia.com/node/274 to make my profile pages.
can u see where i've gone wrong with the use of the code you've given?
thanks
Typo on my part
This line
$sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'");should read$sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";still no joy...
hey - my user profile is called type - uprofile. hence my code now looks like the following, but still see blank screen, can you spot the error? thankyou!
<?php
function phptemplate_username($object, $link = TRUE) {
if ( $object->uid ) {
$type = "uprofile";
$sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";
$nid = db_result(db_query($sql, $object->uid, $type));
if ( $nid ) {
$profile = node_load($nid);
}
}
$name = $profile->field_first_name;
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
// Profile field not set, default to standard behaviour
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $object->name;
}
if ( $link && user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = check_plain($name);
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = l($object->name, $object->homepage);
}
else {
$output = check_plain($object->name);
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
?>
Missing 'if'
Change
$name = $profile->field_first_name;
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
to
$name = $profile->field_first_name;if ( $name ) { // THIS line was missing from your code
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
Also you should remove the closing
?>as that can cause problems if there are spaces after it.no luck, so tried just with profile module
I still had no luck, so i tried starting all over, with the profile module. and having first and last name as profile fields...but frustratingly even this doesn't work! first name and last name display on my user page. but the username is still displaying everywhere.
here's the code i'm putting in - can't see any fault in it?!
<?php
function phptemplate_username($object, $link = TRUE) {
if ( !$object->profile_last_name ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->profile_last_name ) {
$name = $object->profile_last_name;
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
// Profile field not set, default to standard behaviour
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $object->name;
}
if ( $link && user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = check_plain($name);
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = l($object->name, $object->homepage);
}
else {
$output = check_plain($object->name);
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
?>
(i have only put the ?> in here so that it displays nicely here)
can anyone please see whats wrong?
terribly sorry - does work
terribly sorry - this does work - now going to try combining profile module with nodeprofile?? - urgh
first name and last name
I would like to have both first name and last name override the username - and have 2 fields -one for each.
here was my guess/attempt (you can see how bad my php is!!):
function phptemplate_username($object, $link = TRUE) {
if ( !$object->profile_first_name ) && ( !$object->profile_last_name ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->profile_first_name ) && ( $object->profile_last_name ) {
$name = $object->profile_first_name && $object->profile_last_name;
anyone willing to give a helping hand? thankyou
[ Edit: Added <code> and </code> tags around code: nevets ]
Real close
Here it is with some changes
function phptemplate_username($object, $link = TRUE) {
if ( !$object->profile_first_name && !$object->profile_last_name ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->profile_first_name && $object->profile_last_name ) {
$name = $object->profile_first_name . ' ' . $object->profile_last_name;
I placed the 'if' tests inside '(' and ')' and the string concatenation operator is a dot ('.'), the '&&' is for logical ands. I also seperated the first and last name with a space.
Hi again nevets, i've
Hi again nevets,
i've realised that in fact i'm going to have to go back to just using nodeprofile because i need 2 different roles & 2 different registration pages - not sure how to seperate the profile information between the roles with profile module.
Anyhow, I tried your code in my template.php as follows:
<?php
function phptemplate_username($object, $link = TRUE) {
if ( $object->uid ) {
$type = "uprofile";
$sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";
$nid = db_result(db_query($sql, $object->uid, $type));
if ( $nid ) {
$profile = node_load($nid);
}
}
$name = $profile->field_first_name;
if ( $name ) {
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
// Profile field not set, default to standard behaviour
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $object->name;
}
if ( $link && user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = check_plain($name);
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = l($object->name, $object->homepage);
}
else {
$output = check_plain($object->name);
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
?>
and I got the following on my profile page:
warning: htmlspecialchars() expects parameter 1 to be string, array given in /usr/www/htdocs/mysite.com/includes/bootstrap.inc on line 631.
If uprofile is a CCK content type
then
$profile->field_first_name;needs to be$profile->field_first_name[0]['value'];(otherwise you are passing an array to a function that expects a string and hence the error)legend!
nevets you're a legend! thank you ever so much!
my real name is displaying on the tracker list under author- yay!
its not displaying on my buddylist and private message - but i will have a look into these.
Not everything uses the theme function
Some code just displays $user->name directly in which case the theme function has no effect.
ah, i see, so how would i
ah, i see,
so how would i override this in this instance?
sorry - didn't read all of the posts
hey,
sorry - i've just seen that this question has been asked before - i'll have a look at those posts. thanks again for your help!
taking content from fields of 2 types
Hi again nevets,
I have 2 different types of users. I would like to display just first name& last name as the author for a person (as it currently does), but also company name when the author is of the company role. each user has its own content type attached to them - user profile for one and company profile for the other.
so $type = "uprofile" is fine for one role.
how would i adapt the code to also include $type = "cprofile" aswell?
many thanks
figured it out : )
for anyone thats interested - i figured it out - replacing the beginning with:
function phptemplate_username($object, $link = TRUE) {
if ( $object->uid ) {
$user = user_load(array('uid' => $node->uid));
foreach ($user->roles as $rid => $role) {
if ($role == 'Company') {
$type = "cprofile";
} else{$type = "uprofile";
}
}
Small correction
$node->uidshould be$object->uidCorrection: Node profiles cck fields
The above code needs to modified to the following there is an extra closing bracket two lines above
<?php// Profile field not set, default to standard behaviour
?>
See below for corrected code:
<?phpfunction phptemplate_username($object, $link