The following code could be shipped in bio module or included as an example in the README. It uses the nice bio full name instead of username everywhere usernames are shown. There are only 3 lines of custom code here - the rest is copy/paste from core.

/**
 * Use bio module name if available
 *
 * @param $object
 *   The user object to format, usually returned from user_load().
 * @return
 *   A string containing an HTML link to the user's page if the passed object
 *   suggests that this is a site user. Otherwise, only the username is returned.
 */
function phptemplate_username($object) {

  if ($object->uid && $object->name) {
    
    // For bio module.
    if ($nid = bio_for_user($object->uid)) {
      $object->name = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $nid));
    }
    
    // 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 = 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', t('Anonymous'));
  }

  return $output;
}
CommentFileSizeAuthor
#6 blog.module.tar_.gz3.39 KBdugh
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

najibx’s picture

Nice.
It simply takes the bio/node title which is expected to be the fullname. I guess to use this, change "Title" to "Full Name" in the bio content type would be best. No need to create an field for fullname.

Thank you.

webchick’s picture

In D6 I think we could just do this. I'll mull over the best way to document this...

najibx’s picture

It works fine except when writing a comment, since now those 3 lines has override $object->name,
it give an error message to provide valid author. This is the case for core comment module.

Need to put a conditional function here ... many modules looks fine except this one.... which i dunno :-(

TQ

palmergroup’s picture

I was pumped when I found this then I discovered the mentioned error. I put a condition around the code but I hope this will be resolved. It probably wont work for the forum as well.

dugh’s picture

Forgive me but where do we put this code.

dugh’s picture

FileSize
3.39 KB

Oh i see, I added it to the bottom of the template.php file in my theme folder.
This still gives an error that you need a valid username if you try to make a comment while logged in.

Here is code I am using that uses profile fields instead of bio fields.

I don't overwrite $object->name, which I think causes the error with comments (see code near bottom of this thread: http://drupal.org/node/47308 )

Also if you look at the commented out lines, I added a rel nofollow to links, and took out the "not verified" part. Both of these changes appear to already be done for you in drupal 6.

function CHANGETOYOURTHEMENAME_username($object) {

  if ($object->uid && $object->name) {
   
    //Change the below 2 lines if you want to use the bio node title instead of profile fields:
    $thisuser = user_load(array(uid=>$object->uid));
    $fullname = $thisuser->profile_firstname.' '.
                               $thisuser->profile_lastname;

    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($fullname) > 20) {
      $name = drupal_substr($fullname, 0, 15) .'...';
    }
    else {
      $name = $fullname;
    }

    if (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);
      $output = l($object->name, $object->homepage, array('rel' => 'nofollow'));
    }
    else {
      $output = check_plain($object->name);
    }
    //$output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', t('Anonymous'));
  }
  return $output;
}

I also had to hack up modules/blog/blog.module since it displays usernames all over the place instead of a full or real name. Attached is my version until this patch is incorporated into drupal core: http://drupal.org/node/102679

sun’s picture

I'm inclined to mark this won't fix, or move this issue over to User Display API's queue.

fatcrobat’s picture

found an issue with the forum comment validation where "You have to specify a valid author." prompt

this should fix the problem:

function phptemplate_username($object) {

  if ($object->uid && $object->name) {
   
    // For bio module.
    if ($nid = bio_for_user($object->uid)) {
      $object->displayname = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $nid));
    }
	   
    // 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->displayname;
    }

    if (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', t('Anonymous'));
  }

  return $output;
}
</code>
pribeh’s picture

Hey, I've used this to fix most of my problems when it comes to displaying bioname instead of username but the page is still informing the browser of the username as its title. For example, the browser tab for the page will display the username as opposed to the username. Is there a conceivable way to correct this as well?

Prodigy’s picture

@#9

Yes, you need to use THEMENAME_preprocess_page

The snippet below is assuming you are using the core profile module. Just replace the values with the CCK type.

// Map the First and Last name onto the username if requested,
// or if the username appears to be an email address.

$user = user_load($vars['user']->uid);
if ( ($user->profile_displayname == "Given names" || strpos("@", $vars['user']->name) > 0)
&& $vars['title'] == $user->name):
$name = $user->profile_first . " " . $user->profile_last;
$vars['head_title'] = str_replace($vars['title'], $name, $vars['head_title']);
$vars['title'] = $name;
endif;

http://emmajane.net/node/844