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.

  1. What should I write in template.php to get this to work?
  2. Do I have to change the code in username.tpl.php as well?
  3. Is there any other solution to handle this without messing with the templates?

/Magnus

Comments

magnus’s picture

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:

/**
 * Format a username.
 *
 * @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 theme_username($object) {

  // If the user has 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);
    }
  }

  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 (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('attributes' => 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 (!empty($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;
}

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

therainmakor’s picture

Your code is changing the user name and should be something like this

function phptemplate_username($object) {
  if ($object->uid && $object->name) {
    $name = $object->name;
    // If the user has nickname defined and want to use that
    $user = user_load(array(uid => $object->uid));
    if (!empty($user->profile_nickname)) {
      $name = $user->profile_nickname;
    }
    else {
    // If the user has a full name defined, use that
      if (!empty($user->profile_first_name)) {
         $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($name) > 20) {
      $name = drupal_substr($name, 0, 15) .'...';
    }

    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('attributes' => 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 (!empty($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;
}