By magnus on
In profiles I have four fields:
- profile_first_name
- profile_last_name
- profile_nickname
- profile_show_nickname (this is a checkbox)
To change the default output of $user in Drupal 5 I use this template.php:
<?php
/**
* This snippet catches the default user name and looks
* for a username.tpl.php file in the same folder
* which has the new layout.
*/
function phptemplate_username($object) {
if ($object->in_preview) {
return theme_username($object);
}
return _phptemplate_callback('username', array('object' => $object));
}
?>
And in username.tpl.php I have:
<?php
if ($object->uid && $object->name) {
// If the user has a nickname defined and want to use that
$user = user_load(array(uid => $object->uid));
if ($user->profile_show_nickname && !empty($user->profile_nickname)) {
$object->name = $user->profile_nickname;
}
else {
// If the user has a full name defined, use that
if (!empty($user->profile_first_name)) {
$object->name = ($user->profile_first_name . " " . $user->profile_last_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 (user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = $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 = '<a href="/'. $object->homepage .'">'. $object->name .'</a>';
}
else {
$output = $object->name;
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
print $output;
?>
In Drupal 6 I found out that the _phptemplate_callback() is no longer supported, stated here http://drupal.org/node/132442.
- What should I write in template.php to get this to work?
- Do I have to change the code in username.tpl.php as well?
- Is there any other solution to handle this without messing with the templates?
/Magnus
Comments
In includes/theme.inc on row
In includes/theme.inc on row 1543 I found the original function for the output of usernames. I inserted my code for nickname or full name and got almost the same code as I wrote above:
This works great, BUT it also changes my input name when I'm about to post a comment. So the author will not be valid.
If I try to post an article, forum topic node, poll or story it works.
Anyway, now I know that this code also works in Drupal 6. I just need the right call to the function and put it in username.tpl.php
Anyone with a solution?
/Magnus
Code is changing username
Your code is changing the user name and should be something like this